<!DOCTYPE html>
<html lang="zh-cn" dir="ltr">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="基础概念 # 数据库：是一个以某种有组织的方式存储的数据集合。
表：是一种结构化的文件，可用来存储某种特定类型的数据。
模式：
模式可以用来描述数据库中特定的表以及整个数据库（和其中表的关系） 关于数据库和表的布局及特性的信息 列：表中的一个字段。数据库中每个列都有相应的数据类型，数据类型定义列可以存储的数据种类。
行：表中的数据是按行存储的，表中的一个记录。
**主键：**一列（或一组列），其值能够唯一区分表中每个行。主键用来表示一个特定的行。
满足主键的条件
任意两行都不具有相同的主键值； 每个行都必须具有一个主键值（主键列不允许NULL值） 可以一起使用多个列作为主键。
SQL structured query language 结构化查询语言。
数据库的发展史 # 第一代数据库：层次模型、网状模型
层次模型
缺点：
1、 查找不同类的数据效率低了（导航的结构的缺点）
2、 数据不完整（不能区分到底是一个李白还是两个李白）
网状模型
网状模型解决了层次数据的数据不完整的问题，但是没有解决层次模型的导航问题。
关系型数据库
特点：
每个表都是独立的
表与表之间通过公共字段来建立关系
优点：解决了导航问题，并且数据完整性得到解决
缺点：多表查询效率低了
提示：我们现在用的主流的数据库都是关系模型的。
MySql安装 # 在Ubuntu中，默认情况下，只有最新版本的MySQL包含在APT软件包存储库中,要安装它，只需更新服务器上的包索引并安装默认包apt-get。
#命令1 sudo apt-get update #命令2 sudo apt-get install mysql-server 初始化配置 # sudo mysql_secure_installation 配置项较多，如下所示：
#1 VALIDATE PASSWORD PLUGIN can be used to test passwords... Press y|Y for Yes, any other key for No: N (我的选项) #2 Please set the password for root here.">
<meta name="theme-color" content="#FFFFFF">
<meta name="color-scheme" content="light dark"><meta property="og:title" content="MySql基础总结" />
<meta property="og:description" content="基础概念 # 数据库：是一个以某种有组织的方式存储的数据集合。
表：是一种结构化的文件，可用来存储某种特定类型的数据。
模式：
模式可以用来描述数据库中特定的表以及整个数据库（和其中表的关系） 关于数据库和表的布局及特性的信息 列：表中的一个字段。数据库中每个列都有相应的数据类型，数据类型定义列可以存储的数据种类。
行：表中的数据是按行存储的，表中的一个记录。
**主键：**一列（或一组列），其值能够唯一区分表中每个行。主键用来表示一个特定的行。
满足主键的条件
任意两行都不具有相同的主键值； 每个行都必须具有一个主键值（主键列不允许NULL值） 可以一起使用多个列作为主键。
SQL structured query language 结构化查询语言。
数据库的发展史 # 第一代数据库：层次模型、网状模型
层次模型
缺点：
1、 查找不同类的数据效率低了（导航的结构的缺点）
2、 数据不完整（不能区分到底是一个李白还是两个李白）
网状模型
网状模型解决了层次数据的数据不完整的问题，但是没有解决层次模型的导航问题。
关系型数据库
特点：
每个表都是独立的
表与表之间通过公共字段来建立关系
优点：解决了导航问题，并且数据完整性得到解决
缺点：多表查询效率低了
提示：我们现在用的主流的数据库都是关系模型的。
MySql安装 # 在Ubuntu中，默认情况下，只有最新版本的MySQL包含在APT软件包存储库中,要安装它，只需更新服务器上的包索引并安装默认包apt-get。
#命令1 sudo apt-get update #命令2 sudo apt-get install mysql-server 初始化配置 # sudo mysql_secure_installation 配置项较多，如下所示：
#1 VALIDATE PASSWORD PLUGIN can be used to test passwords... Press y|Y for Yes, any other key for No: N (我的选项) #2 Please set the password for root here." />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.org/docs/%E6%95%B0%E6%8D%AE%E5%BA%93/mysql/2021-04-20-mysql%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93/" /><meta property="article:section" content="docs" />
<meta property="article:published_time" content="2021-04-20T18:07:08+00:00" />
<meta property="article:modified_time" content="2021-04-20T18:07:08+00:00" />

<title>MySql基础总结 | Soulmate</title>
<link rel="manifest" href="/manifest.json">
<link rel="icon" href="/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="/book.min.c58292d36b18b675680ab9baea2029204537b839ea72f258746ec0f32ce8d6c8.css" integrity="sha256-xYKS02sYtnVoCrm66iApIEU3uDnqcvJYdG7A8yzo1sg=" crossorigin="anonymous">
  <script defer src="/flexsearch.min.js"></script>
  <script defer src="/en.search.min.beb4601b1eab618a95e3b0d49ce0487f50ed4cb8eecc3a3bd5f798bdba5ad9f3.js" integrity="sha256-vrRgGx6rYYqV47DUnOBIf1DtTLjuzDo71feYvbpa2fM=" crossorigin="anonymous"></script>
<!--
Made with Book Theme
https://github.com/alex-shpak/hugo-book
-->
  
</head>
<body dir="ltr">
  <input type="checkbox" class="hidden toggle" id="menu-control" />
  <input type="checkbox" class="hidden toggle" id="toc-control" />
  <main class="container flex">
    <aside class="book-menu">
      <div class="book-menu-content">
        
  <nav>
<h2 class="book-brand">
  <a class="flex align-center" href="/"><img src="/logo.png" alt="Logo" /><span>Soulmate</span>
  </a>
</h2>


<div class="book-search">
  <input type="text" id="book-search-input" placeholder="Search" aria-label="Search" maxlength="64" data-hotkeys="s/" />
  <div class="book-search-spinner hidden"></div>
  <ul id="book-search-results"></ul>
</div>












  



  
  <ul>
    
      
        <li class="book-section-flat" >
          
  
  

  
    <input type="checkbox" id="section-074eed0fb8c6f9d99dfeff898388792f" class="toggle"  />
    <label for="section-074eed0fb8c6f9d99dfeff898388792f" class="flex justify-between">
      <a role="button" class="">计算机基础</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/git%E5%9F%BA%E7%A1%80/" class="">Git基础</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/linux%E5%9F%BA%E7%A1%80/" class="">Linux基础</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%9F%BA%E7%A1%80/" class="">操作系统基础</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E7%A1%80/" class="">数据库基础</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C%E5%9F%BA%E7%A1%80/" class="">计算机网络基础</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li class="book-section-flat" >
          
  
  

  
    <span>Golang</span>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-670eda717a06f31447c52422ec93a159" class="toggle"  />
    <label for="section-670eda717a06f31447c52422ec93a159" class="flex justify-between">
      <a role="button" class="">基础</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/golang/%E5%9F%BA%E7%A1%80/2021-04-07-go%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B8%80/" class="">2021 04 07 Go语言基础（一）</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/golang/%E5%9F%BA%E7%A1%80/2021-10-26-go%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%BA%8C/" class="">2021 10 26 Go语言基础（二）</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/golang/%E5%9F%BA%E7%A1%80/2021-12-20-benchmark%E6%B5%8B%E8%AF%95/" class="">2021 12 20 Benchmark测试</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/golang/%E5%9F%BA%E7%A1%80/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-go/" class="">数据结构 Go</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-c453881a62c640f6a4cceec99c02477e" class="toggle"  />
    <label for="section-c453881a62c640f6a4cceec99c02477e" class="flex justify-between">
      <a role="button" class="">高阶</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/golang/%E9%AB%98%E9%98%B6/go%E8%AF%AD%E8%A8%80%E9%AB%98%E9%98%B6%E4%B8%80/" class="">Go语言高阶（一）</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-ee53da12118bfd3e9b35f3e9494ab55b" class="toggle"  />
    <label for="section-ee53da12118bfd3e9b35f3e9494ab55b" class="flex justify-between">
      <a role="button" class="">LeetCode</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/golang/leetcode/2021-10-14-golang%E5%8A%9B%E6%89%A3%E5%88%B7%E9%A2%98%E4%B8%80/" class="">2021 10 14 Golang力扣刷题（一）</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/golang/leetcode/2021-10-28-leetcode%E7%AE%97%E6%B3%95%E6%80%BB%E7%BB%93/" class="">2021 10 28 Leet Code算法总结</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/golang/leetcode/2021-11-04-golang%E5%8A%9B%E6%89%A3%E5%88%B7%E9%A2%98%E4%BA%8C/" class="">2021 11 04 Golang力扣刷题（二）</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/golang/leetcode/%E5%BF%85%E5%88%B7top101/" class="">必刷top101</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li class="book-section-flat" >
          
  
  

  
    <span>区块链</span>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-76bfb65f46ce25a0ff78d6cc4ad47773" class="toggle"  />
    <label for="section-76bfb65f46ce25a0ff78d6cc4ad47773" class="flex justify-between">
      <a role="button" class="">Fabric</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/2021-05-08-fabric-sdk-go%E8%AF%A6%E8%A7%A3/" class="">fabric-sdk-go详解</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-1e3df3eda5cf08a9c7e8cc723288fefb" class="toggle"  />
    <label for="section-1e3df3eda5cf08a9c7e8cc723288fefb" class="flex justify-between">
      <a role="button" class="">环境测试</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95/2021-03-18-centos%E5%AE%89%E8%A3%85fabric1.2/" class="">centos安装fabric1.2</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95/2021-03-22-fabric%E7%BD%91%E7%BB%9C%E4%B8%AD%E7%9A%84%E6%8A%A5%E9%94%99%E4%BA%8C/" class="">2021 03 22 Fabric网络中的报错（二）</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95/2021-03-24-fabric%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/" class="">2021 03 24 Fabric环境搭建</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95/2021-03-25-fabric-solo%E8%8A%82%E7%82%B9%E6%B5%8B%E8%AF%95/" class="">2021 03 25 Fabric Solo节点测试</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95/2021-03-25-fabric%E5%A4%9A%E6%9C%BA%E6%90%AD%E5%BB%BA/" class="">2021 03 25 Fabric多机搭建</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95/2021-05-01-%E6%89%8B%E5%8A%A8%E7%94%9F%E6%88%90ca%E8%AF%81%E4%B9%A6%E6%90%AD%E5%BB%BAfabric%E7%BD%91%E7%BB%9C/" class="">2021 05 01 手动生成ca证书搭建fabric网络</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95/2021-12-20-%E9%83%A8%E7%BD%B2tape%E6%B5%8B%E8%AF%95/" class="">2021 12 20 部署tape测试</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95/fabric%E7%BD%91%E7%BB%9C%E4%B8%AD%E7%9A%84%E6%8A%A5%E9%94%99%E4%B8%80/" class="">Fabric网络中的报错（一）</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-25aef1d59a561fcefcaecb043ef8afd2" class="toggle"  />
    <label for="section-25aef1d59a561fcefcaecb043ef8afd2" class="flex justify-between">
      <a role="button" class="">配置文件</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6/2021-03-29-configtx-yaml%E6%96%87%E4%BB%B6%E8%AF%A6%E8%A7%A3/" class="">2021 03 29 Configtx Yaml文件详解</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6/2021-03-29-crypto-config-yaml%E6%96%87%E4%BB%B6%E8%AF%A6%E8%A7%A3/" class="">2021 03 29 Crypto Config Yaml文件详解</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6/2021-03-30-config-yaml%E6%96%87%E4%BB%B6%E8%AF%A6%E8%A7%A3/" class="">2021 03 30 Config Yaml文件详解</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6/2021-03-30-docker-compose-yaml%E6%96%87%E4%BB%B6%E8%AF%A6%E8%A7%A3/" class="">2021 03 30 Docker Compose Yaml文件详解</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6/2021-05-01-cryptogen%E7%94%9F%E6%88%90%E7%9A%84%E8%AF%81%E4%B9%A6%E8%AF%A6%E8%A7%A3/" class="">2021 05 01 Cryptogen生成的证书详解</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/2022-04-14-%E5%8D%87%E7%BA%A7%E9%93%BE%E7%A0%81/" class="">升级链码</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/2021-04-15-fabric-ca%E8%AF%A6%E8%A7%A3/" class="">2021 04 15 Fabric Ca详解</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/2021-04-15-fabric1.4%E5%A4%9A%E9%80%9A%E9%81%93%E5%AE%9E%E9%AA%8C/" class="">2021 04 15 Fabric1.4多通道实验</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/2021-04-17-%E5%A6%82%E4%BD%95%E5%9C%A8%E5%B7%B2%E6%9C%89%E7%BB%84%E7%BB%87%E4%B8%AD%E5%A2%9E%E5%8A%A0%E8%8A%82%E7%82%B9/" class="">2021 04 17 如何在已有组织中增加节点</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/2021-05-02-fabric%E6%B5%8F%E8%A7%88%E5%99%A8%E6%90%AD%E5%BB%BA/" class="">2021 05 02 Fabric浏览器搭建</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/2021-05-10-%E6%99%BA%E8%83%BD%E5%90%88%E7%BA%A6/" class="">2021 05 10 智能合约</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/2022-02-25-fabric%E7%9B%B8%E5%85%B3%E6%9C%BA%E5%88%B6%E4%B8%8E%E5%8E%9F%E7%90%86/" class="">2022 02 25 Fabric相关机制与原理</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/fabric/2022-03-25-%E5%8C%BA%E5%9D%97%E9%93%BE%E7%BD%91%E7%BB%9C%E6%B7%BB%E5%8A%A0%E7%BB%84%E7%BB%87/" class="">2022 03 25 区块链网络添加组织</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-b07621083aa30b12de4c0333941e479f" class="toggle"  />
    <label for="section-b07621083aa30b12de4c0333941e479f" class="flex justify-between">
      <a role="button" class="">比特币</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/%E6%AF%94%E7%89%B9%E5%B8%81/2022-02-25-%E6%AF%94%E7%89%B9%E5%B8%81%E7%9B%B8%E5%85%B3%E6%9C%BA%E5%88%B6%E4%B8%8E%E5%8E%9F%E7%90%86/" class="">2022 02 25 比特币相关机制与原理</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-ff5477999ef29208270c84e8b56b2758" class="toggle"  />
    <label for="section-ff5477999ef29208270c84e8b56b2758" class="flex justify-between">
      <a role="button" class="">IPFS</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/ipfs/2021-06-02-ipfs%E7%A7%81%E6%9C%89%E7%BD%91%E7%BB%9C%E6%90%AD%E5%BB%BA/" class="">2021 06 02 IP Fs私有网络搭建</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/ipfs/2021-07-08-ipfs%E5%9F%BA%E6%9C%AC%E5%8E%9F%E7%90%86%E4%B8%80/" class="">2021 07 08 IP Fs基本原理（一）</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/ipfs/2021-07-12-ipfs-webui%E5%8F%AF%E8%A7%86%E5%8C%96%E5%B7%A5%E5%85%B7%E6%90%AD%E5%BB%BA/" class="">2021 07 12 Ipfs Webui可视化工具搭建</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/ipfs/2021-12-05-go-ipfs-api/" class="">2021 12 05 Go Ipfs API</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-77a0a17b06e8d04cfb0fba9952941706" class="toggle"  />
    <label for="section-77a0a17b06e8d04cfb0fba9952941706" class="flex justify-between">
      <a role="button" class="">密码学</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/%E5%AF%86%E7%A0%81%E5%AD%A6/2021-03-04-%E5%AF%86%E7%A0%81%E5%AD%A6%E5%9F%BA%E7%A1%80/" class="">2021 03 04 密码学基础</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/%E5%AF%86%E7%A0%81%E5%AD%A6/2021-04-12-%E6%A4%AD%E5%9C%86%E6%9B%B2%E7%BA%BF%E5%8A%A0%E5%AF%86/" class="">2021 04 12 椭圆曲线加密</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/%E5%AF%86%E7%A0%81%E5%AD%A6/2022-08-15-%E5%8C%BA%E5%9D%97%E9%93%BE%E5%AE%89%E5%85%A8%E5%9F%BA%E7%A1%80/" class="">2022 08 15 区块链安全基础</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-2e4d10b323fd668021dbd532575d8790" class="toggle"  />
    <label for="section-2e4d10b323fd668021dbd532575d8790" class="flex justify-between">
      <a role="button" class="">Docker</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/docker/2021-04-30-docker%E5%B8%B8%E7%94%A8%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93/" class="">2021 04 30 Docker常用知识总结</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/docker/docker%E5%9F%BA%E7%A1%80/" class="">Docker基础</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-70383d7f28a7ae4bf840a844eb61aa16" class="toggle"  />
    <label for="section-70383d7f28a7ae4bf840a844eb61aa16" class="flex justify-between">
      <a role="button" class="">共识算法</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/%E5%85%B1%E8%AF%86%E7%AE%97%E6%B3%95/2022-03-26-raft%E5%85%B1%E8%AF%86%E7%AE%97%E6%B3%95/" class="">2022 03 26 Raft共识算法</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E5%8C%BA%E5%9D%97%E9%93%BE/%E5%85%B1%E8%AF%86%E7%AE%97%E6%B3%95/%E5%85%B1%E8%AF%86%E7%AE%97%E6%B3%95%E5%9F%BA%E7%A1%80/" class="">共识算法基础</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li class="book-section-flat" >
          
  
  

  
    <span>数据库</span>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-94f26238d1ca3b5bcd40eadc7a88d726" class="toggle" checked />
    <label for="section-94f26238d1ca3b5bcd40eadc7a88d726" class="flex justify-between">
      <a role="button" class="">MySql</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E6%95%B0%E6%8D%AE%E5%BA%93/mysql/2021-04-20-mysql%E5%9F%BA%E7%A1%80%E6%80%BB%E7%BB%93/" class="active">MySql基础总结</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E6%95%B0%E6%8D%AE%E5%BA%93/mysql/mac%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E6%89%80%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98/" class="">Mac连接数据库所遇到的问题</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li>
          
  
  

  
    <input type="checkbox" id="section-01e4ef5e6295826a1eab8f1a7114ec2c" class="toggle"  />
    <label for="section-01e4ef5e6295826a1eab8f1a7114ec2c" class="flex justify-between">
      <a role="button" class="">Redis</a>
    </label>
  

          
  <ul>
    
      
        <li>
          
  
  

  
    <a href="/docs/%E6%95%B0%E6%8D%AE%E5%BA%93/redis/2022-03-21-redis%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA/" class="">Redis集群搭建</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E6%95%B0%E6%8D%AE%E5%BA%93/redis/2022-03-20-redis%E5%9F%BA%E7%A1%80/" class="">Redis基础</a>
  

        </li>
      
    
      
        <li>
          
  
  

  
    <a href="/docs/%E6%95%B0%E6%8D%AE%E5%BA%93/redis/2021-05-02-redis%E9%9D%A2%E8%AF%95%E6%80%BB%E7%BB%93/" class="">redis面试总结</a>
  

        </li>
      
    
  </ul>

        </li>
      
    
  </ul>

        </li>
      
    
      
        <li class="book-section-flat" >
          
  
  

  
    <input type="checkbox" id="section-5f7b667081fbae0581cd216f66d5102f" class="toggle"  />
    <label for="section-5f7b667081fbae0581cd216f66d5102f" class="flex justify-between">
      <a role="button" class="">博客</a>
    </label>
  

          
  <ul>
    
  </ul>

        </li>
      
    
  </ul>











  
<ul>
  
  <li>
    <a href="https://gitee.com/chaincode" target="_blank" rel="noopener">
        Gitee
      </a>
  </li>
  
</ul>






</nav>




  <script>(function(){var e=document.querySelector("aside .book-menu-content");addEventListener("beforeunload",function(){localStorage.setItem("menu.scrollTop",e.scrollTop)}),e.scrollTop=localStorage.getItem("menu.scrollTop")})()</script>


 
      </div>
    </aside>

    <div class="book-page">
      <header class="book-header">
        
  <div class="flex align-center justify-between">
  <label for="menu-control">
    <img src="/svg/menu.svg" class="book-icon" alt="Menu" />
  </label>

  <strong>MySql基础总结</strong>

  <label for="toc-control">
    
    <img src="/svg/toc.svg" class="book-icon" alt="Table of Contents" />
    
  </label>
</div>


  
  <aside class="hidden clearfix">
    
  
<nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#基础概念">基础概念</a>
          <ul>
            <li><a href="#数据库的发展史">数据库的发展史</a></li>
          </ul>
        </li>
        <li><a href="#mysql安装">MySql安装</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#数据库操作">数据库操作</a>
          <ul>
            <li><a href="#显示数据库">显示数据库</a></li>
            <li><a href="#创建数据库">创建数据库</a></li>
            <li><a href="#heading"></a></li>
            <li><a href="#删除数据库">删除数据库</a></li>
            <li><a href="#显示创建数据库的sql语句">显示创建数据库的SQL语句</a></li>
            <li><a href="#修改数据库">修改数据库</a></li>
            <li><a href="#选择数据库">选择数据库</a></li>
          </ul>
        </li>
        <li><a href="#表的操作">表的操作</a>
          <ul>
            <li><a href="#显示所有表">显示所有表</a></li>
            <li><a href="#创建表">创建表</a></li>
            <li><a href="#显示创建表的语句">显示创建表的语句</a></li>
            <li><a href="#查看表结构">查看表结构</a></li>
            <li><a href="#删除表">删除表</a></li>
            <li><a href="#修改表">修改表</a></li>
            <li><a href="#复制表">复制表</a></li>
          </ul>
        </li>
        <li><a href="#数据操作">数据操作</a>
          <ul>
            <li><a href="#插入数据">插入数据</a></li>
            <li><a href="#更新数据">更新数据</a></li>
            <li><a href="#删除数据">删除数据</a></li>
            <li><a href="#清空表">清空表</a></li>
            <li><a href="#查询表">查询表</a></li>
          </ul>
        </li>
        <li><a href="#sql分类">SQL分类</a>
          <ul>
            <li><a href="#数据表的文件介绍">数据表的文件介绍</a></li>
            <li><a href="#字符集">字符集</a></li>
          </ul>
        </li>
        <li><a href="#数据类型">数据类型</a>
          <ul>
            <li><a href="#值类型">值类型</a></li>
            <li><a href="#字符型">字符型</a></li>
            <li><a href="#枚举enum">枚举（enum）</a></li>
            <li><a href="#集合set">集合（set）</a></li>
            <li><a href="#日期类型">日期类型</a></li>
            <li><a href="#boolean">boolean</a></li>
          </ul>
        </li>
        <li><a href="#列属性">列属性</a>
          <ul>
            <li><a href="#null--not-null">(null | not null)</a></li>
            <li><a href="#默认值default">默认值default</a></li>
            <li><a href="#自动增长">自动增长</a></li>
            <li><a href="#主键">主键</a></li>
            <li><a href="#唯一键">唯一键</a></li>
            <li><a href="#备注comment">备注comment</a></li>
          </ul>
        </li>
        <li><a href="#数据完整性介绍">数据完整性介绍</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#引用完整性">引用完整性</a>
          <ul>
            <li></li>
            <li><a href="#外键操作">外键操作</a></li>
          </ul>
        </li>
        <li><a href="#数据库基本概念">数据库基本概念</a></li>
        <li><a href="#实体和实体之间的关系">实体和实体之间的关系</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#数据库设计的步骤">数据库设计的步骤</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#数据规范化">数据规范化</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#查询语句">查询语句</a>
          <ul>
            <li><a href="#字段表达式">字段表达式</a></li>
            <li><a href="#from子句">from子句</a></li>
            <li><a href="#dual表">dual表</a></li>
            <li><a href="#where子句">where子句</a></li>
            <li><a href="#in--not-in">in | not in</a></li>
            <li><a href="#betweenandnot-betweenand">between…and|not between…and</a></li>
            <li><a href="#is-null--is-not-null">is null | is not null</a></li>
            <li><a href="#聚合函数">聚合函数</a></li>
            <li><a href="#通配符">通配符</a></li>
            <li><a href="#模糊查询like">模糊查询（like）</a></li>
            <li><a href="#order-by排序">order by排序</a></li>
            <li><a href="#group-by-分组查询">group by 【分组查询】</a></li>
            <li><a href="#having条件">having条件</a></li>
            <li><a href="#limit">limit</a></li>
            <li><a href="#查询语句中的选项">查询语句中的选项</a></li>
          </ul>
        </li>
        <li><a href="#union联合">union（联合）</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#多表查询分类">多表查询分类</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#子查询">子查询</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#视图view">视图【view】</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#事务transaction">事务transaction</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#索引index">索引【index】</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#函数">函数</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#预处理">预处理</a></li>
        <li><a href="#存储过程procedure">存储过程procedure</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#go连接mysql">GO连接MySQL</a></li>
      </ul>
    </li>
  </ul>
</nav>



  </aside>
  
 
      </header>

      
      
  <article class="markdown"><p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032248478.png" alt="" /></p>
<h2 id="基础概念">
  基础概念
  <a class="anchor" href="#%e5%9f%ba%e7%a1%80%e6%a6%82%e5%bf%b5">#</a>
</h2>
<blockquote>
<p>数据库：是一个以某种有组织的方式存储的数据集合。</p>
<p>表：是一种结构化的文件，可用来存储某种特定类型的数据。</p>
<p>模式：</p>
<ul>
<li>模式可以用来描述数据库中特定的表以及整个数据库（和其中表的关系）</li>
<li>关于数据库和表的布局及特性的信息</li>
</ul>
<p>列：表中的一个字段。数据库中每个列都有相应的数据类型，数据类型定义列可以存储的数据种类。</p>
<p>行：表中的数据是按行存储的，表中的一个记录。</p>
<p>**主键：**一列（或一组列），其值能够唯一区分表中每个行。主键用来表示一个特定的行。</p>
<p>满足主键的条件</p>
<ul>
<li>任意两行都不具有相同的主键值；</li>
<li>每个行都必须具有一个主键值（主键列不允许NULL值）</li>
</ul>
<p>可以一起使用多个列作为主键。</p>
<p>SQL   structured query language  结构化查询语言。</p>
</blockquote>
<h3 id="数据库的发展史">
  数据库的发展史
  <a class="anchor" href="#%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e5%8f%91%e5%b1%95%e5%8f%b2">#</a>
</h3>
<blockquote>
<p>第一代数据库：层次模型、网状模型</p>
<p><strong>层次模型</strong></p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032248426.png" alt="" /></p>
<p>缺点：</p>
<p>1、     查找不同类的数据效率低了（导航的结构的缺点）</p>
<p>2、     数据不完整（不能区分到底是一个李白还是两个李白）</p>
<p><strong>网状模型</strong></p>
<p>网状模型解决了层次数据的数据不完整的问题，但是没有解决层次模型的导航问题。</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032249662.png" alt="" /></p>
<p><strong>关系型数据库</strong></p>
<p>特点：</p>
<ol>
<li>
<p>每个表都是独立的</p>
</li>
<li>
<p>表与表之间通过公共字段来建立关系</p>
</li>
<li>
<p>优点：解决了导航问题，并且数据完整性得到解决</p>
<p>缺点：多表查询效率低了</p>
<p>提示：我们现在用的主流的数据库都是关系模型的。</p>
</li>
</ol>
</blockquote>
<h2 id="mysql安装">
  MySql安装
  <a class="anchor" href="#mysql%e5%ae%89%e8%a3%85">#</a>
</h2>
<blockquote>
<p>在<code>Ubuntu</code>中，默认情况下，只有最新版本的<code>MySQL</code>包含在<code>APT</code>软件包存储库中,要安装它，只需更新服务器上的包索引并安装默认包<code>apt-get</code>。</p>
<pre tabindex="0"><code>#命令1
sudo apt-get update
#命令2
sudo apt-get install mysql-server
</code></pre></blockquote>
<h4 id="初始化配置">
  初始化配置
  <a class="anchor" href="#%e5%88%9d%e5%a7%8b%e5%8c%96%e9%85%8d%e7%bd%ae">#</a>
</h4>
<pre tabindex="0"><code>sudo mysql_secure_installation
</code></pre><p>配置项较多，如下所示：</p>
<pre tabindex="0"><code>#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)
#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)
#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)
#4
Normally, root should only be allowed to connect from
&#39;localhost&#39;. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项)
#5
By default, MySQL comes with a database named &#39;test&#39; that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)
#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)
</code></pre><h4 id="检查mysql服务状态">
  检查mysql服务状态
  <a class="anchor" href="#%e6%a3%80%e6%9f%a5mysql%e6%9c%8d%e5%8a%a1%e7%8a%b6%e6%80%81">#</a>
</h4>
<pre tabindex="0"><code>systemctl status mysql.service
</code></pre><p>显示如下结果说明mysql服务是正常的：</p>
<pre tabindex="0"><code>● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:&gt;
     Active: active (running) since Mon 2021-06-28 20:03:54 CST; 2min 21s ago
   Main PID: 62268 (mysqld)
     Status: &#34;Server is operational&#34;
      Tasks: 38 (limit: 2307)
     Memory: 326.2M
     CGroup: /system.slice/mysql.service
             └─62268 /usr/sbin/mysqld

6月 28 20:03:53 ubuntu systemd[1]: Starting MySQL Community Server...
6月 28 20:03:54 ubuntu systemd[1]: Started MySQL Community Server.
lines 1-12/12 (END)
</code></pre><h4 id="通过命令行启动关闭">
  通过命令行启动\关闭
  <a class="anchor" href="#%e9%80%9a%e8%bf%87%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%90%af%e5%8a%a8%e5%85%b3%e9%97%ad">#</a>
</h4>
<pre tabindex="0"><code>启动
使用 service 启动：service mysql start
使用 mysqld 脚本启动：/etc/inint.d/mysql start
使用 safe_mysqld 启动：safe_mysql&amp;
停止
使用 service 启动：service mysql stop
使用 mysqld 脚本启动：/etc/inint.d/mysql stop
mysqladmin shutdown
重启
使用 service 启动：service mysql restart
使用 mysqld 脚本启动：/etc/inint.d/mysql restart
</code></pre><h4 id="连接服务器">
  连接服务器
  <a class="anchor" href="#%e8%bf%9e%e6%8e%a5%e6%9c%8d%e5%8a%a1%e5%99%a8">#</a>
</h4>
<p>通过命令行面板连接</p>
<pre tabindex="0"><code>host：主机			-h
username：用户名	-u
password：密码		-p
port：端口			-P
</code></pre><pre tabindex="0"><code>mysql -h127.0.0.1 -P3306 -u root -ptian3281916
或者
mysql -u root -p
如果MySQL服务器在本地，IP地址可以省略；如果MySQL服务器用的是3306端口，-P也是可以省略
</code></pre><blockquote>
<p>报错：</p>
<p>mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1698 (28000): Access denied for user &lsquo;root&rsquo;@&rsquo;localhost&rsquo;</p>
<p>解决办法：</p>
<p>sudo mysql -u root -p</p>
</blockquote>
<p>关闭连接</p>
<pre tabindex="0"><code>方法一：exit
方法二：quit
方法三：\q
</code></pre><h2 id="数据库操作">
  数据库操作
  <a class="anchor" href="#%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c">#</a>
</h2>
<h3 id="显示数据库">
  显示数据库
  <a class="anchor" href="#%e6%98%be%e7%a4%ba%e6%95%b0%e6%8d%ae%e5%ba%93">#</a>
</h3>
<pre tabindex="0"><code>mysql&gt; show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.57 sec)
</code></pre><p>安装MySQL后，MySQL自带了4个数据库</p>
<ol>
<li>information_schema：存储了MySQL服务器管理数据库的信息。</li>
<li>performance_schema：MySQL5.5新增的表，用来保存数据库服务器性能的参数</li>
<li>mysql：MySQL系统数据库，保存的登录用户名，密码，以及每个用户的权限等等</li>
<li>sys：通过这个库可以快速的了解系统的元数据信息。</li>
</ol>
<h3 id="创建数据库">
  创建数据库
  <a class="anchor" href="#%e5%88%9b%e5%bb%ba%e6%95%b0%e6%8d%ae%e5%ba%93">#</a>
</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">create</span> <span style="color:#66d9ef">database</span> [<span style="color:#66d9ef">if</span> <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">exists</span>] <span style="color:#f92672">`</span><span style="color:#960050;background-color:#1e0010">数据名</span><span style="color:#f92672">`</span> [<span style="color:#960050;background-color:#1e0010">字符编码</span>]
</span></span></code></pre></div><p>创建数据库：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">database</span> stu;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">09</span> sec)
</span></span></code></pre></div><p>创建数据库的时候判断一下数据库是否存在，如果不存在再创建</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">database</span> <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">exists</span> stu;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row affected, <span style="color:#ae81ff">1</span> <span style="color:#a6e22e">warning</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>如果数据库名是关键字和特殊字符要报错</p>
<p>解决：在特殊字符、关键字行加上反引号</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">database</span> <span style="color:#f92672">`</span><span style="color:#66d9ef">create</span><span style="color:#f92672">`</span>;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">05</span> sec)
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#a6e22e">多学一招：为了创建数据库时万无一失，我们可以在所有的数据库名上加上反引号</span>
</span></span></code></pre></div><p>创建数据库的时候可以指定字符编码</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">database</span> teacher <span style="color:#66d9ef">charset</span><span style="color:#f92672">=</span>gbk;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">01</span> sec)
</span></span><span style="display:flex;"><span>gbk		<span style="color:#960050;background-color:#1e0010">简体中文</span>
</span></span><span style="display:flex;"><span>gb2312<span style="color:#960050;background-color:#1e0010">：</span>	<span style="color:#960050;background-color:#1e0010">简体中文</span>
</span></span><span style="display:flex;"><span>utf8<span style="color:#960050;background-color:#1e0010">：</span>	<span style="color:#960050;background-color:#1e0010">通用字符编码</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#a6e22e">脚下留心：创建数据库如果不指定字符编码，默认和MySQL服务器的字符编码是一致的。</span>
</span></span></code></pre></div><h3 id="heading">
  
  <a class="anchor" href="#heading">#</a>
</h3>
<h3 id="删除数据库">
  删除数据库
  <a class="anchor" href="#%e5%88%a0%e9%99%a4%e6%95%b0%e6%8d%ae%e5%ba%93">#</a>
</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">database</span> [<span style="color:#66d9ef">if</span> <span style="color:#66d9ef">exists</span>] <span style="color:#960050;background-color:#1e0010">数据库名</span>
</span></span></code></pre></div><p>删除数据库</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">database</span> teacher;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>如果删除的数据库不存在，会报错</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">database</span> teacher;
</span></span><span style="display:flex;"><span>ERROR <span style="color:#ae81ff">1008</span> (HY000): Can<span style="color:#e6db74">&#39;t drop database &#39;</span>teacher<span style="color:#e6db74">&#39;; database doesn&#39;</span>t exist
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span>
</span></span></code></pre></div><p>解决：删除之前判断一下，如果存在就删除</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">database</span> <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">exists</span> teacher;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows affected, <span style="color:#ae81ff">1</span> <span style="color:#a6e22e">warning</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="显示创建数据库的sql语句">
  显示创建数据库的SQL语句
  <a class="anchor" href="#%e6%98%be%e7%a4%ba%e5%88%9b%e5%bb%ba%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84sql%e8%af%ad%e5%8f%a5">#</a>
</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">database</span> <span style="color:#960050;background-color:#1e0010">数据库名</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">database</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------------------------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">Database</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Create</span> <span style="color:#66d9ef">Database</span>                                              <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------------------------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stu      <span style="color:#f92672">|</span> <span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">DATABASE</span> <span style="color:#f92672">`</span>stu<span style="color:#f92672">`</span> <span style="color:#75715e">/*!40100 DEFAULT CHARACTER SET utf8 */</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------------------------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">01</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">database</span> teacher;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+-----------------------------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">Database</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Create</span> <span style="color:#66d9ef">Database</span>                                                 <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+-----------------------------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> teacher  <span style="color:#f92672">|</span> <span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">DATABASE</span> <span style="color:#f92672">`</span>teacher<span style="color:#f92672">`</span> <span style="color:#75715e">/*!40100 DEFAULT CHARACTER SET gbk */</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+-----------------------------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="修改数据库">
  修改数据库
  <a class="anchor" href="#%e4%bf%ae%e6%94%b9%e6%95%b0%e6%8d%ae%e5%ba%93">#</a>
</h3>
<p>修改数据库的字符编码</p>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">database</span> <span style="color:#960050;background-color:#1e0010">数据库名</span> <span style="color:#66d9ef">charset</span><span style="color:#f92672">=</span><span style="color:#960050;background-color:#1e0010">字符编码</span>
</span></span></code></pre></div><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">database</span> teacher <span style="color:#66d9ef">charset</span><span style="color:#f92672">=</span>utf8;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">database</span> teacher;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------------------------------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">Database</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Create</span> <span style="color:#66d9ef">Database</span>                                                  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------------------------------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> teacher  <span style="color:#f92672">|</span> <span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">DATABASE</span> <span style="color:#f92672">`</span>teacher<span style="color:#f92672">`</span> <span style="color:#75715e">/*!40100 DEFAULT CHARACTER SET utf8 */</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------------------------------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="选择数据库">
  选择数据库
  <a class="anchor" href="#%e9%80%89%e6%8b%a9%e6%95%b0%e6%8d%ae%e5%ba%93">#</a>
</h3>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">use</span> <span style="color:#960050;background-color:#1e0010">数据库名</span>
</span></span></code></pre></div><p>选择数据库</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">use</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">Database</span> changed
</span></span></code></pre></div><h2 id="表的操作">
  表的操作
  <a class="anchor" href="#%e8%a1%a8%e7%9a%84%e6%93%8d%e4%bd%9c">#</a>
</h2>
<h3 id="显示所有表">
  显示所有表
  <a class="anchor" href="#%e6%98%be%e7%a4%ba%e6%89%80%e6%9c%89%e8%a1%a8">#</a>
</h3>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">show</span> <span style="color:#66d9ef">tables</span>;
</span></span></code></pre></div><h3 id="创建表">
  创建表
  <a class="anchor" href="#%e5%88%9b%e5%bb%ba%e8%a1%a8">#</a>
</h3>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> [<span style="color:#66d9ef">if</span> <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">exists</span>] <span style="color:#960050;background-color:#1e0010">表名</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">字段名</span> <span style="color:#960050;background-color:#1e0010">数据类型</span> [<span style="color:#66d9ef">null</span><span style="color:#f92672">|</span><span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span>] [<span style="color:#66d9ef">auto_increment</span>] [<span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>] [comment],
</span></span><span style="display:flex;"><span>    <span style="color:#960050;background-color:#1e0010">字段名</span> <span style="color:#960050;background-color:#1e0010">数据类型</span> [<span style="color:#66d9ef">default</span>]<span style="color:#960050;background-color:#1e0010">…</span>
</span></span><span style="display:flex;"><span>)<span style="color:#66d9ef">engine</span><span style="color:#f92672">=</span><span style="color:#960050;background-color:#1e0010">存储引擎</span>
</span></span></code></pre></div><p>单词</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span>   	<span style="color:#960050;background-color:#1e0010">空</span><span style="color:#f92672">|</span><span style="color:#960050;background-color:#1e0010">非空</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">default</span>	          	<span style="color:#960050;background-color:#1e0010">默认值</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">auto_increment</span>    	<span style="color:#960050;background-color:#1e0010">自动增长</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>       	<span style="color:#960050;background-color:#1e0010">主键</span>
</span></span><span style="display:flex;"><span>comment           	<span style="color:#960050;background-color:#1e0010">备注</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">engine</span>           	<span style="color:#960050;background-color:#1e0010">引擎</span>   innodb  myisam  memory  <span style="color:#960050;background-color:#1e0010">引擎是决定数据存储的方式</span>
</span></span></code></pre></div><p>创建简单的表</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">set</span> names gbk;   <span style="color:#75715e"># 设置字符编码   带中文的表先设置这个
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">database</span> itcast;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">use</span> itcast;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">Database</span> changed
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">tables</span>;
</span></span><span style="display:flex;"><span>Empty <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">05</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 创建表
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stu</span>(
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>,
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">30</span>)<span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">字符串</span> <span style="color:#960050;background-color:#1e0010">长度</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">13</span> sec)
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 查看创建的表
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">tables</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Tables_in_itcast <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stu              <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------+</span>
</span></span></code></pre></div><p>创建复杂的表</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">set</span> names gbk;   <span style="color:#75715e"># 设置字符编码
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">05</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">exists</span> <span style="color:#a6e22e">teacher</span>(
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span> comment <span style="color:#e6db74">&#39;主键&#39;</span>,
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span> comment <span style="color:#e6db74">&#39;姓名&#39;</span>,
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> phone <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) comment <span style="color:#e6db74">&#39;电话号码&#39;</span>,
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> <span style="color:#f92672">`</span><span style="color:#66d9ef">add</span><span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">100</span>) <span style="color:#66d9ef">default</span> <span style="color:#e6db74">&#39;地址不详&#39;</span> comment <span style="color:#e6db74">&#39;地址&#39;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> )<span style="color:#66d9ef">engine</span><span style="color:#f92672">=</span>innodb;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">09</span> sec)
</span></span></code></pre></div><p>多学一招：create table 数据库名.表名，用于给指定的数据库创建表</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> data.<span style="color:#a6e22e">stu</span>(  <span style="color:#75715e">#给data数据库中创建stu表
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>   <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>,
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>));
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="显示创建表的语句">
  显示创建表的语句
  <a class="anchor" href="#%e6%98%be%e7%a4%ba%e5%88%9b%e5%bb%ba%e8%a1%a8%e7%9a%84%e8%af%ad%e5%8f%a5">#</a>
</h3>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#960050;background-color:#1e0010">表名</span>
</span></span></code></pre></div><p>显示创建teacher表的语句</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> teacher;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------------------------------------------------------------------</span><span style="color:#75715e">--
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">-------------------------------------------------------------------------------------
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">-----------------------------------------------------------------------------------</span><span style="color:#75715e">--
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">-------+
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">|</span> <span style="color:#66d9ef">Table</span>   <span style="color:#f92672">|</span> <span style="color:#66d9ef">Create</span> <span style="color:#66d9ef">Table</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------------------------------------------------------------------</span><span style="color:#75715e">--
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">-------------------------------------------------------------------------------------
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">-----------------------------------------------------------------------------------</span><span style="color:#75715e">--
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">-------+
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">|</span> teacher <span style="color:#f92672">|</span> <span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">TABLE</span> <span style="color:#f92672">`</span>teacher<span style="color:#f92672">`</span> (
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">`</span>id<span style="color:#f92672">`</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>) <span style="color:#66d9ef">NOT</span> <span style="color:#66d9ef">NULL</span> <span style="color:#66d9ef">AUTO_INCREMENT</span> COMMENT <span style="color:#e6db74">&#39;主键&#39;</span>,
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">`</span>name<span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">NOT</span> <span style="color:#66d9ef">NULL</span> COMMENT <span style="color:#e6db74">&#39;姓名&#39;</span>,
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">`</span>phone<span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">NULL</span> COMMENT <span style="color:#e6db74">&#39;电话号码&#39;</span>,
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">`</span><span style="color:#66d9ef">add</span><span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">100</span>) <span style="color:#66d9ef">DEFAULT</span> <span style="color:#e6db74">&#39;地址不详&#39;</span> COMMENT <span style="color:#e6db74">&#39;地址&#39;</span>,
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">PRIMARY</span> <span style="color:#66d9ef">KEY</span> (<span style="color:#f92672">`</span>id<span style="color:#f92672">`</span>)
</span></span><span style="display:flex;"><span>) <span style="color:#66d9ef">ENGINE</span><span style="color:#f92672">=</span>InnoDB <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">CHARSET</span><span style="color:#f92672">=</span>utf8                         <span style="color:#f92672">|</span>
</span></span></code></pre></div><p>将两个字段竖着排列           show create table <code>表名</code>\G</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> teacher<span style="color:#960050;background-color:#1e0010">\</span>G;
</span></span><span style="display:flex;"><span><span style="color:#f92672">***************************</span> <span style="color:#ae81ff">1</span>. row <span style="color:#f92672">***************************</span>
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">Table</span>: teacher
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">Create</span> <span style="color:#66d9ef">Table</span>: <span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">TABLE</span> <span style="color:#f92672">`</span>teacher<span style="color:#f92672">`</span> (
</span></span><span style="display:flex;"><span> <span style="color:#f92672">`</span>id<span style="color:#f92672">`</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>) <span style="color:#66d9ef">NOT</span> <span style="color:#66d9ef">NULL</span> <span style="color:#66d9ef">AUTO_INCREMENT</span> COMMENT <span style="color:#e6db74">&#39;主键&#39;</span>,
</span></span><span style="display:flex;"><span> <span style="color:#f92672">`</span>name<span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">NOT</span> <span style="color:#66d9ef">NULL</span> COMMENT <span style="color:#e6db74">&#39;姓名&#39;</span>,
</span></span><span style="display:flex;"><span> <span style="color:#f92672">`</span>phone<span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">NULL</span> COMMENT <span style="color:#e6db74">&#39;电话号码&#39;</span>,
</span></span><span style="display:flex;"><span> <span style="color:#f92672">`</span><span style="color:#66d9ef">add</span><span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">100</span>) <span style="color:#66d9ef">DEFAULT</span> <span style="color:#e6db74">&#39;地址不详&#39;</span> COMMENT <span style="color:#e6db74">&#39;地址&#39;</span>,
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">PRIMARY</span> <span style="color:#66d9ef">KEY</span> (<span style="color:#f92672">`</span>id<span style="color:#f92672">`</span>)
</span></span><span style="display:flex;"><span>) <span style="color:#66d9ef">ENGINE</span><span style="color:#f92672">=</span>InnoDB <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">CHARSET</span><span style="color:#f92672">=</span>utf8
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="查看表结构">
  查看表结构
  <a class="anchor" href="#%e6%9f%a5%e7%9c%8b%e8%a1%a8%e7%bb%93%e6%9e%84">#</a>
</h3>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">desc</span>[ribe] <span style="color:#960050;background-color:#1e0010">表名</span>
</span></span></code></pre></div><p>查看teacher表的结构</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">describe</span> teacher;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type         <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span>  <span style="color:#f92672">|</span> Extra          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)      <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span> PRI <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)  <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> phone <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)  <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">add</span>   <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">100</span>) <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">地址不详</span>  <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">4</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">08</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> teacher;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type         <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span>  <span style="color:#f92672">|</span> Extra          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)      <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span> PRI <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)  <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> phone <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)  <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">add</span>   <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">100</span>) <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">地址不详</span>  <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">4</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">01</span> sec)
</span></span></code></pre></div><h3 id="删除表">
  删除表
  <a class="anchor" href="#%e5%88%a0%e9%99%a4%e8%a1%a8">#</a>
</h3>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">table</span> [<span style="color:#66d9ef">if</span> <span style="color:#66d9ef">exists</span>] <span style="color:#960050;background-color:#1e0010">表</span><span style="color:#ae81ff">1</span><span style="color:#960050;background-color:#1e0010">，表</span><span style="color:#ae81ff">2</span>,<span style="color:#960050;background-color:#1e0010">…</span> 
</span></span></code></pre></div><p>删除表</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">table</span> stu;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">08</span> sec)
</span></span></code></pre></div><p>如果删除一个不存在的表就会报错，删除的时候可以判断一下，存在就删除。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">table</span> stu;
</span></span><span style="display:flex;"><span>ERROR <span style="color:#ae81ff">1051</span> (<span style="color:#ae81ff">42</span>S02): Unknown <span style="color:#66d9ef">table</span> <span style="color:#e6db74">&#39;stu&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">table</span> <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">exists</span> stu;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows affected, <span style="color:#ae81ff">1</span> <span style="color:#a6e22e">warning</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>可以一次删除多个表</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">table</span> a1,a2;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="修改表">
  修改表
  <a class="anchor" href="#%e4%bf%ae%e6%94%b9%e8%a1%a8">#</a>
</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> <span style="color:#960050;background-color:#1e0010">表名</span>
</span></span></code></pre></div><p>1、添加字段：alter table 表名add [column] 字段名 数据类型 [位置]</p>
<h4 id="添加字段">
  添加字段
  <a class="anchor" href="#%e6%b7%bb%e5%8a%a0%e5%ad%97%e6%ae%b5">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> teacher <span style="color:#66d9ef">add</span> age <span style="color:#66d9ef">int</span>;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">09</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> teacher;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type         <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span>  <span style="color:#f92672">|</span> Extra          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)      <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span> PRI <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)  <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> phone <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)  <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">add</span>   <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">100</span>) <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">地址不详</span>  <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> age   <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)      <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">5</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h5 id="在第一个位置上添加字段">
  在第一个位置上添加字段
  <a class="anchor" href="#%e5%9c%a8%e7%ac%ac%e4%b8%80%e4%b8%aa%e4%bd%8d%e7%bd%ae%e4%b8%8a%e6%b7%bb%e5%8a%a0%e5%ad%97%e6%ae%b5">#</a>
</h5>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> teacher <span style="color:#66d9ef">add</span> email <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">30</span>) first;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> teacher;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type         <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span>  <span style="color:#f92672">|</span> Extra          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> email <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">30</span>)  <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)      <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span> PRI <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)  <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> phone <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)  <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">add</span>   <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">100</span>) <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">地址不详</span>  <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> age   <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)      <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span></code></pre></div><h5 id="在指定的字段后添加字段">
  在指定的字段后添加字段
  <a class="anchor" href="#%e5%9c%a8%e6%8c%87%e5%ae%9a%e7%9a%84%e5%ad%97%e6%ae%b5%e5%90%8e%e6%b7%bb%e5%8a%a0%e5%ad%97%e6%ae%b5">#</a>
</h5>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> teacher <span style="color:#66d9ef">add</span> sex <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">2</span>) after name;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> teacher;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type         <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span>  <span style="color:#f92672">|</span> Extra          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> email <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">30</span>)  <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)      <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span> PRI <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)  <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> sex   <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">2</span>)   <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> phone <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)  <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">add</span>   <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">100</span>) <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">地址不详</span>   <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> age   <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)      <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">7</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="删除字段">
  删除字段：
  <a class="anchor" href="#%e5%88%a0%e9%99%a4%e5%ad%97%e6%ae%b5">#</a>
</h4>
<p>alter table 表 drop [column] 字段名</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> teacher <span style="color:#66d9ef">drop</span> email;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h4 id="修改字段改名改类型">
  修改字段(改名改类型)：
  <a class="anchor" href="#%e4%bf%ae%e6%94%b9%e5%ad%97%e6%ae%b5%e6%94%b9%e5%90%8d%e6%94%b9%e7%b1%bb%e5%9e%8b">#</a>
</h4>
<p>alter table 表 change [column] 原字段名 新字段名 数据类型 …</p>
<p>将字段sex改为xingbie，数据类型为int</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> teacher <span style="color:#66d9ef">change</span> sex xingbie <span style="color:#66d9ef">int</span>;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h4 id="修改字段不改名">
  修改字段（不改名）:
  <a class="anchor" href="#%e4%bf%ae%e6%94%b9%e5%ad%97%e6%ae%b5%e4%b8%8d%e6%94%b9%e5%90%8d">#</a>
</h4>
<p>alter table 表 modify 字段名 字段属性…</p>
<p>将性别的数据类型改为varchar(2)</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> teacher modify xingbie <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">2</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h4 id="修改引擎">
  修改引擎：
  <a class="anchor" href="#%e4%bf%ae%e6%94%b9%e5%bc%95%e6%93%8e">#</a>
</h4>
<p>alter table 表名 engine=引擎名</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> teacher <span style="color:#66d9ef">engine</span><span style="color:#f92672">=</span>myisam;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">05</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h4 id="修改表名">
  修改表名：
  <a class="anchor" href="#%e4%bf%ae%e6%94%b9%e8%a1%a8%e5%90%8d">#</a>
</h4>
<p>alter table 表名 rename to 新表名</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> teacher <span style="color:#66d9ef">rename</span> <span style="color:#66d9ef">to</span> stu;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">tables</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Tables_in_itcast <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stu              <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="复制表">
  复制表
  <a class="anchor" href="#%e5%a4%8d%e5%88%b6%e8%a1%a8">#</a>
</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法一：</span><span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#960050;background-color:#1e0010">新表</span> <span style="color:#66d9ef">select</span> <span style="color:#960050;background-color:#1e0010">字段</span> <span style="color:#66d9ef">from</span> <span style="color:#960050;background-color:#1e0010">旧表</span>
</span></span></code></pre></div><p>特点：不能复制父表的主键，能够复制父表的数据</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> stu1 <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">1</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu1;    <span style="color:#75715e"># 查看数据复制到新表中
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+----+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id <span style="color:#f92672">|</span> name <span style="color:#f92672">|</span> addr <span style="color:#f92672">|</span> score <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>  <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> rose <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>    <span style="color:#f92672">|</span>    <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> stu1;    <span style="color:#75715e">#  主键没有复制
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+-------+-------------+------+-----+----------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type        <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span>  <span style="color:#f92672">|</span> Extra <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------------+------+-----+----------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)     <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#ae81ff">0</span>        <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> addr  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">50</span>) <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">地址不详</span>        <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> score <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)     <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------------+------+-----+----------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">4</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法二：</span><span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#960050;background-color:#1e0010">新表</span> <span style="color:#66d9ef">like</span> <span style="color:#960050;background-color:#1e0010">旧表</span>
</span></span></code></pre></div><p>特点：只能复制表结构，不能复制表数据</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu2;   <span style="color:#75715e"># 数据没有复制
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Empty <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">01</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> stu2;   <span style="color:#75715e"># 主键复制了
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+-------+-------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type        <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span>  <span style="color:#f92672">|</span> Extra          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)     <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span> PRI <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> addr  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">50</span>) <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">地址不详</span>        <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> score <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)     <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------------+------+-----+----------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">4</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h2 id="数据操作">
  数据操作
  <a class="anchor" href="#%e6%95%b0%e6%8d%ae%e6%93%8d%e4%bd%9c">#</a>
</h2>
<p>创建测试表</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stu</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span> comment <span style="color:#e6db74">&#39;主键&#39;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> addr <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">50</span>) <span style="color:#66d9ef">default</span> <span style="color:#e6db74">&#39;地址不详&#39;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> score <span style="color:#66d9ef">int</span> comment <span style="color:#e6db74">&#39;成绩&#39;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">01</span> sec)
</span></span></code></pre></div><h3 id="插入数据">
  插入数据
  <a class="anchor" href="#%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae">#</a>
</h3>
<h4 id="插入一条数据">
  插入一条数据
  <a class="anchor" href="#%e6%8f%92%e5%85%a5%e4%b8%80%e6%9d%a1%e6%95%b0%e6%8d%ae">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> <span style="color:#960050;background-color:#1e0010">表名</span> (<span style="color:#960050;background-color:#1e0010">字段名</span>, <span style="color:#960050;background-color:#1e0010">字段名</span>,<span style="color:#960050;background-color:#1e0010">…</span>) <span style="color:#66d9ef">values</span> (<span style="color:#960050;background-color:#1e0010">值</span><span style="color:#ae81ff">1</span>, <span style="color:#960050;background-color:#1e0010">值</span><span style="color:#ae81ff">1</span>,<span style="color:#960050;background-color:#1e0010">…</span>)
</span></span></code></pre></div><p>例题一：插入数据</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> <span style="color:#a6e22e">stu</span> (id,name,addr,score) <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1</span>,<span style="color:#e6db74">&#39;tom&#39;</span>,<span style="color:#e6db74">&#39;上海&#39;</span>,<span style="color:#ae81ff">88</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">11</span> sec)
</span></span></code></pre></div><p>例题二：插入的字段可以和表的字段顺序不一致。值的顺序必须和插入字段的顺序一致。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> <span style="color:#a6e22e">stu</span> (name,score,addr,id) <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;berry&#39;</span>,<span style="color:#ae81ff">77</span>,<span style="color:#e6db74">&#39;北京&#39;</span>,<span style="color:#ae81ff">2</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>例题三：可以插入部分字段，但是，非空字段必须插入</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> <span style="color:#a6e22e">stu</span> (id,name,addr) <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">3</span>,<span style="color:#e6db74">&#39;ketty&#39;</span>,<span style="color:#e6db74">&#39;上海&#39;</span>);
</span></span></code></pre></div><p>例题四：自动增长字段不用插入，数据库会自动插入增长的数字</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> <span style="color:#a6e22e">stu</span> (name,addr) <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;rose&#39;</span>,<span style="color:#e6db74">&#39;北京&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>例题五：自动增长列的值插入null即可</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> <span style="color:#a6e22e">stu</span> (id,name,addr,score) <span style="color:#66d9ef">values</span> (<span style="color:#66d9ef">null</span>,<span style="color:#e6db74">&#39;李白&#39;</span>,<span style="color:#e6db74">&#39;上海&#39;</span>,<span style="color:#ae81ff">66</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>例题六：插入值的顺序和个数与表字段的顺序和个数一致，插入的字段可以省略</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> stu <span style="color:#66d9ef">values</span> (<span style="color:#66d9ef">null</span>,<span style="color:#e6db74">&#39;杜甫&#39;</span>,<span style="color:#e6db74">&#39;北京&#39;</span>,<span style="color:#66d9ef">null</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>例题七：通过default关键字插入默认值</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> stu <span style="color:#66d9ef">values</span> (<span style="color:#66d9ef">null</span>,<span style="color:#e6db74">&#39;李清照&#39;</span>,<span style="color:#66d9ef">default</span>,<span style="color:#ae81ff">66</span>);
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">脚下留心：</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span><span style="color:#960050;background-color:#1e0010">、插入字段的顺序与值的顺序必须一致</span>
</span></span></code></pre></div><h4 id="插入多条数据">
  插入多条数据
  <a class="anchor" href="#%e6%8f%92%e5%85%a5%e5%a4%9a%e6%9d%a1%e6%95%b0%e6%8d%ae">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> stu <span style="color:#66d9ef">values</span> (<span style="color:#66d9ef">null</span>,<span style="color:#e6db74">&#39;辛弃疾&#39;</span>,<span style="color:#66d9ef">default</span>,<span style="color:#ae81ff">66</span>),(<span style="color:#66d9ef">null</span>,<span style="color:#e6db74">&#39;岳飞&#39;</span>,<span style="color:#e6db74">&#39;河南&#39;</span>,<span style="color:#ae81ff">77</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">2</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">2</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h3 id="更新数据">
  更新数据
  <a class="anchor" href="#%e6%9b%b4%e6%96%b0%e6%95%b0%e6%8d%ae">#</a>
</h3>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">update</span> <span style="color:#960050;background-color:#1e0010">表名</span> <span style="color:#66d9ef">set</span> <span style="color:#960050;background-color:#1e0010">字段</span><span style="color:#f92672">=</span><span style="color:#960050;background-color:#1e0010">值</span> [<span style="color:#66d9ef">where</span> <span style="color:#960050;background-color:#1e0010">条件</span>] 
</span></span></code></pre></div><p>例题一：将1号学生的地址改成山东</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">update</span> stu <span style="color:#66d9ef">set</span> addr<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;山东&#39;</span> <span style="color:#66d9ef">where</span> id<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span>
</span></span></code></pre></div><p>例题二：将ketty的成绩改为99</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">update</span> stu <span style="color:#66d9ef">set</span> score<span style="color:#f92672">=</span><span style="color:#ae81ff">99</span> <span style="color:#66d9ef">where</span> name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;ketty&#39;</span>;
</span></span></code></pre></div><p>例题三：将berry地址改成上海，成绩改成66</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">update</span> stu <span style="color:#66d9ef">set</span> addr<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;上海&#39;</span>,score<span style="color:#f92672">=</span><span style="color:#ae81ff">66</span> <span style="color:#66d9ef">where</span> name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;berry&#39;</span>;
</span></span></code></pre></div><p>例题四：将上海的学生成绩改为60</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">update</span> stu <span style="color:#66d9ef">set</span> score<span style="color:#f92672">=</span><span style="color:#ae81ff">60</span> <span style="color:#66d9ef">where</span> addr<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;上海&#39;</span>;
</span></span></code></pre></div><p>例题五：条件可以省略，如果省略，更改所有数据（将所有数据的地址改为湖南，成绩改为70）</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">update</span> stu <span style="color:#66d9ef">set</span> addr<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;湖南&#39;</span>,score<span style="color:#f92672">=</span><span style="color:#ae81ff">70</span>;
</span></span></code></pre></div><p>例题六：将2、3的学生成绩改为65</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">update</span> stu <span style="color:#66d9ef">set</span> score<span style="color:#f92672">=</span><span style="color:#ae81ff">65</span> <span style="color:#66d9ef">where</span> id<span style="color:#f92672">=</span><span style="color:#ae81ff">2</span> <span style="color:#66d9ef">or</span> id<span style="color:#f92672">=</span><span style="color:#ae81ff">3</span>;
</span></span></code></pre></div><h3 id="删除数据">
  删除数据
  <a class="anchor" href="#%e5%88%a0%e9%99%a4%e6%95%b0%e6%8d%ae">#</a>
</h3>
<p>语法</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">delete</span> <span style="color:#66d9ef">from</span> <span style="color:#960050;background-color:#1e0010">表名</span> [<span style="color:#66d9ef">where</span> <span style="color:#960050;background-color:#1e0010">条件</span>] 
</span></span></code></pre></div><p>例题一：删除学号是1号的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">delete</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> id<span style="color:#f92672">=</span><span style="color:#ae81ff">1</span>;
</span></span></code></pre></div><p>例题二：删除成绩小于等于65分的</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">delete</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> score<span style="color:#f92672">&lt;=</span><span style="color:#ae81ff">65</span>;
</span></span></code></pre></div><p>例题三：删除表中所有记录</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">delete</span> <span style="color:#66d9ef">from</span> stu;
</span></span></code></pre></div><h3 id="清空表">
  清空表
  <a class="anchor" href="#%e6%b8%85%e7%a9%ba%e8%a1%a8">#</a>
</h3>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>truncate <span style="color:#66d9ef">table</span> <span style="color:#960050;background-color:#1e0010">表名</span>
</span></span></code></pre></div><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> truncate <span style="color:#66d9ef">table</span> stu;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><pre tabindex="0"><code>脚下留心：delete from 表和truncate table 表区别？
delete from 表：遍历表记录，一条一条的删除
truncate table：将原表销毁，再创建一个同结构的新表。就清空表而言，这种方法效率高。
</code></pre><h3 id="查询表">
  查询表
  <a class="anchor" href="#%e6%9f%a5%e8%af%a2%e8%a1%a8">#</a>
</h3>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">select</span> <span style="color:#960050;background-color:#1e0010">列名</span> <span style="color:#66d9ef">from</span> <span style="color:#960050;background-color:#1e0010">表</span>
</span></span></code></pre></div><p>例题：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> name,score <span style="color:#66d9ef">from</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name <span style="color:#f92672">|</span> score <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> rose <span style="color:#f92672">|</span>    <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> id,name,addr,score <span style="color:#66d9ef">from</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id <span style="color:#f92672">|</span> name <span style="color:#f92672">|</span> addr <span style="color:#f92672">|</span> score <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>  <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> rose <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>  <span style="color:#f92672">|</span>    <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu;  <span style="color:#75715e"># *表示所有字段
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+----+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id <span style="color:#f92672">|</span> name <span style="color:#f92672">|</span> addr <span style="color:#f92672">|</span> score <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>  <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> rose <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>  <span style="color:#f92672">|</span>    <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h2 id="sql分类">
  SQL分类
  <a class="anchor" href="#sql%e5%88%86%e7%b1%bb">#</a>
</h2>
<p>DDL（data definition language）数据库定义语言CREATE、ALTER、DROP、SHOW</p>
<p>DML（data manipulation language）数据操纵语言SELECT、UPDATE、INSERT、DELETE</p>
<p>DCL（Data Control Language）数据库控制语言,是用来设置或更改数据库用户或角色权限的语句</p>
<h3 id="数据表的文件介绍">
  数据表的文件介绍
  <a class="anchor" href="#%e6%95%b0%e6%8d%ae%e8%a1%a8%e7%9a%84%e6%96%87%e4%bb%b6%e4%bb%8b%e7%bb%8d">#</a>
</h3>
<p>一个数据库对应一个文件夹</p>
<p>一个表对应一个或多个文件</p>
<p>引擎是myisam，一个表对应三个文件</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032250591.png" alt="" /></p>
<p>引擎是innodb,一个表对应一个表结构文件</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032250694.png" alt="" /></p>
<p>所有的innodb引擎的数据统一的存放在data\ibdata1文件中。如果数据量很大，MySQL会自动的创建ibdata2，ibdata3，…，目的就是为了便于管理。</p>
<p>引擎是memory，数据存储在内存中，重启服务数据丢失，但是读取速度非常快。</p>
<h3 id="字符集">
  字符集
  <a class="anchor" href="#%e5%ad%97%e7%ac%a6%e9%9b%86">#</a>
</h3>
<p>字符集：字符在保存和传输时对应的二进制编码集合。</p>
<p>创建测试数据库</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stu</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>插入中文报错</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032250330.png" alt="" /></p>
<p>多学一招：我们只要设置“set names 字符编码”，就可以更改character_set_client、character_set_results的值。</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032251712.png" alt="" /></p>
<h2 id="数据类型">
  数据类型
  <a class="anchor" href="#%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b">#</a>
</h2>
<h3 id="值类型">
  值类型
  <a class="anchor" href="#%e5%80%bc%e7%b1%bb%e5%9e%8b">#</a>
</h3>
<h4 id="整型">
  整型
  <a class="anchor" href="#%e6%95%b4%e5%9e%8b">#</a>
</h4>
<table>
<thead>
<tr>
<th style="text-align:center">类型</th>
<th style="text-align:center">字节</th>
<th style="text-align:center">范围</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">tinyint</td>
<td style="text-align:center">1</td>
<td style="text-align:center">-128~127</td>
</tr>
<tr>
<td style="text-align:center">smallint</td>
<td style="text-align:center">2</td>
<td style="text-align:center">-32768~32767</td>
</tr>
<tr>
<td style="text-align:center">mediumint</td>
<td style="text-align:center">3</td>
<td style="text-align:center">-8388608~8388607</td>
</tr>
<tr>
<td style="text-align:center">int</td>
<td style="text-align:center">4</td>
<td style="text-align:center">-2^31^~2^31^-1</td>
</tr>
<tr>
<td style="text-align:center">bigint</td>
<td style="text-align:center">8</td>
<td style="text-align:center">-2^63^~2^63^-1</td>
</tr>
</tbody>
</table>
<p>1、无符号整数（unsigned）：无符号数没有负数，正数部分是有符号的两倍。</p>
<p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stu</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">smallint</span> <span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span> comment <span style="color:#e6db74">&#39;主键&#39;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> age <span style="color:#66d9ef">tinyint</span> <span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span> comment <span style="color:#e6db74">&#39;年龄&#39;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> money <span style="color:#66d9ef">bigint</span> <span style="color:#66d9ef">unsigned</span> comment <span style="color:#e6db74">&#39;存款&#39;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+----------------------+------+-----+---------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type                 <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span> <span style="color:#f92672">|</span> Extra          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+----------------------+------+-----+---------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">smallint</span>(<span style="color:#ae81ff">5</span>) <span style="color:#66d9ef">unsigned</span> <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span> PRI <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> age   <span style="color:#f92672">|</span> <span style="color:#66d9ef">tinyint</span>(<span style="color:#ae81ff">3</span>) <span style="color:#66d9ef">unsigned</span>  <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> money <span style="color:#f92672">|</span> <span style="color:#66d9ef">bigint</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">unsigned</span>  <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>                <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+----------------------+------+-----+---------+----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">3</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span>, <span style="color:#ae81ff">3</span> <span style="color:#a6e22e">warnings</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>2、整型支持显示宽度（最小的显示位数）  比如int(5)，如果数值的位数小于5位，前面加上前导0。比如输入12，显示00012；大于5位就不添加前导0。</p>
<pre tabindex="0"><code>脚下留心：必须结合zerofill才起作用
</code></pre><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stu</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">5</span>),
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> age <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">5</span>) <span style="color:#66d9ef">zerofill</span>   <span style="color:#75715e"># 填充前导0
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type                     <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span> <span style="color:#f92672">|</span> Extra <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">5</span>)                   <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> age   <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">5</span>) <span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">zerofill</span> <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+--------------------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> stu <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">11</span>);
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> stu <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1111111</span>,<span style="color:#ae81ff">2222222</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id      <span style="color:#f92672">|</span> age     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">00011</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">1111111</span> <span style="color:#f92672">|</span> <span style="color:#ae81ff">2222222</span> <span style="color:#f92672">|</span>    <span style="color:#75715e"># 注意：age填充了前导0
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+---------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="浮点型保存近似值小数">
  浮点型（保存近似值小数）
  <a class="anchor" href="#%e6%b5%ae%e7%82%b9%e5%9e%8b%e4%bf%9d%e5%ad%98%e8%bf%91%e4%bc%bc%e5%80%bc%e5%b0%8f%e6%95%b0">#</a>
</h4>
<table>
<thead>
<tr>
<th style="text-align:center">浮点型</th>
<th style="text-align:center">占用字节</th>
<th style="text-align:center">范围</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">float（单精度）</td>
<td style="text-align:center">4</td>
<td style="text-align:center">-3.4E+38~3.4E+38</td>
</tr>
<tr>
<td style="text-align:center">double（双精度）</td>
<td style="text-align:center">8</td>
<td style="text-align:center">-1.8E+308~1.8E+308</td>
</tr>
</tbody>
</table>
<p>1、浮点数声明:  float(M,D)   double(M,D)</p>
<p>M：总位数</p>
<p>D：小数位数</p>
<p>例题；</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t1</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> num1 <span style="color:#66d9ef">float</span>(<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">2</span>),   <span style="color:#75715e">#总位数是5，小数位数是2，那么整数位数是3，
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> num2 <span style="color:#66d9ef">double</span>(<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">1</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">08</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t1 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1</span>.<span style="color:#ae81ff">23</span>,<span style="color:#ae81ff">1</span>.<span style="color:#ae81ff">23</span>); <span style="color:#75715e">#如果精度超出了允许的范围，会四舍五入
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t1;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> num1 <span style="color:#f92672">|</span> num2 <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">1</span>.<span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>  <span style="color:#ae81ff">1</span>.<span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span>   <span style="color:#75715e">#如果精度超出了允许的范围，会四舍五入
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>2、浮点的精度可能会丢失【精度指的是小数】</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032251235.png" alt="" /></p>
<h4 id="定点数">
  定点数
  <a class="anchor" href="#%e5%ae%9a%e7%82%b9%e6%95%b0">#</a>
</h4>
<p>语法：decimal(M,D)</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t4</span>(
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> num <span style="color:#66d9ef">decimal</span>(<span style="color:#ae81ff">20</span>,<span style="color:#ae81ff">19</span>)
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t4 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1</span>.<span style="color:#ae81ff">1234567890123456789</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">01</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t4;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> num                   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">1</span>.<span style="color:#ae81ff">1234567890123456789</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">多学一招：</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span><span style="color:#960050;background-color:#1e0010">、定点数是变长的，大致每</span><span style="color:#ae81ff">9</span><span style="color:#960050;background-color:#1e0010">个数字用</span><span style="color:#ae81ff">4</span><span style="color:#960050;background-color:#1e0010">个字节来存储。定点数之所以能保存精确的小数，因为整数和小数是分开存储的。占用的资源比浮点数要多。</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span><span style="color:#960050;background-color:#1e0010">、定点数和浮点数都支持显示宽度和无符号数。</span>
</span></span></code></pre></div><h3 id="字符型">
  字符型
  <a class="anchor" href="#%e5%ad%97%e7%ac%a6%e5%9e%8b">#</a>
</h3>
<table>
<thead>
<tr>
<th style="text-align:center">数据类型</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">长度</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">char(长度)</td>
<td style="text-align:center">定长</td>
<td style="text-align:center">最大255</td>
</tr>
<tr>
<td style="text-align:center">varchar(长度)</td>
<td style="text-align:center">变长</td>
<td style="text-align:center">最大65535</td>
</tr>
<tr>
<td style="text-align:center">tinytext</td>
<td style="text-align:center">大段文本</td>
<td style="text-align:center">2^8^-1=255</td>
</tr>
<tr>
<td style="text-align:center">text</td>
<td style="text-align:center">大段文本</td>
<td style="text-align:center">2^16^-1=65535</td>
</tr>
<tr>
<td style="text-align:center">mediumtext</td>
<td style="text-align:center">大段文本</td>
<td style="text-align:center">2^24^-1</td>
</tr>
<tr>
<td style="text-align:center">longtext</td>
<td style="text-align:center">大段文本</td>
<td style="text-align:center">2^32^-1</td>
</tr>
</tbody>
</table>
<p>1、char(10)和varchar(10)的区别？</p>
<p>答：相同点：它们最多只能保存10个字符；</p>
<pre><code>    不同点：char不回收多余的字符，varchar会回收多余的字符。

                    char效率高，浪费空间，varchar节省空间，效率比char低。
</code></pre>
<p>2、char的最大长度是255。</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032252972.png" alt="" /></p>
<p>3、varchar理论长度是65535字节,实际根本达不到。具体长度与字符编码有关。</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032252736.png" alt="" /></p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032252243.png" alt="" /></p>
<p>4、一个记录的总长度不能超过65535个字节。</p>
<p>5、大块文本（text）不计算在总长度中,一个大块文本只占用10个字节来保存文本的地址。</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032253491.png" alt="" /></p>
<h3 id="枚举enum">
  枚举（enum）
  <a class="anchor" href="#%e6%9e%9a%e4%b8%beenum">#</a>
</h3>
<p>1、从集合中选择一个数据（单选）</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t8</span>(
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>),
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> sex <span style="color:#66d9ef">enum</span>(<span style="color:#e6db74">&#39;男&#39;</span>,<span style="color:#e6db74">&#39;女&#39;</span>,<span style="color:#e6db74">&#39;保密&#39;</span>)   <span style="color:#75715e"># 枚举
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>   <span style="color:#f92672">-&gt;</span> )<span style="color:#66d9ef">charset</span><span style="color:#f92672">=</span>utf8;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t8 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;tom&#39;</span>,<span style="color:#e6db74">&#39;男&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t8 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;berry&#39;</span>,<span style="color:#e6db74">&#39;女&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">05</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t8 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;rose&#39;</span>,<span style="color:#e6db74">&#39;未知&#39;</span>);   <span style="color:#75715e"># 报错，只能插入枚举值
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>ERROR <span style="color:#ae81ff">1265</span> (<span style="color:#ae81ff">01000</span>): Data truncated <span style="color:#66d9ef">for</span> <span style="color:#66d9ef">column</span> <span style="color:#e6db74">&#39;sex&#39;</span> at row <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t8;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> sex  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> tom   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> berry <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+------+</span>
</span></span></code></pre></div><p>2、MySQL的枚举类型是通过整数来管理的，第一个值是1，第二个值是2，以此类推。</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032253300.png" alt="" /></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> sex<span style="color:#f92672">+</span><span style="color:#ae81ff">0</span> <span style="color:#66d9ef">from</span> t8;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> sex<span style="color:#f92672">+</span><span style="color:#ae81ff">0</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>     <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>     <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+</span>
</span></span></code></pre></div><p>3、既然枚举在数据库内部存储的是整数，那么可以直接插入数字</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t8 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;rose&#39;</span>,<span style="color:#ae81ff">3</span>);  <span style="color:#75715e"># 可以直接插入数字
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t8;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> sex  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> tom   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> berry <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> rose  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">保密</span>  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">3</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>枚举的优点：</p>
<p>1、     运行速度快（数字比字符串运算速度快）</p>
<p>2、     限制数据，保证数据完整性</p>
<p>3、     节省空间</p>
<pre tabindex="0"><code>思考：已知枚举占用2个字节，请问最多有多少个枚举值？

答：2个字节=16位，可以保存数字（0-65535），枚举是从1开始，所以枚举最多可以有65535个枚举值。
</code></pre><h3 id="集合set">
  集合（set）
  <a class="anchor" href="#%e9%9b%86%e5%90%88set">#</a>
</h3>
<p>从集合中选择一些数据（多选）</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t9</span>(
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> hobby <span style="color:#66d9ef">set</span>(<span style="color:#e6db74">&#39;爬山&#39;</span>,<span style="color:#e6db74">&#39;读书&#39;</span>,<span style="color:#e6db74">&#39;游泳&#39;</span>,<span style="color:#e6db74">&#39;敲代码&#39;</span>)
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">08</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t9 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;爬山&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t9 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;爬山,游泳&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t9 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;游泳,爬山&#39;</span>);  <span style="color:#75715e"># 插入顺序不一样，但是显示的顺序是一样的
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t9 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;爬山,游泳,开车&#39;</span>);  <span style="color:#75715e"># 报错，插入集合中没有的选项会报错
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>ERROR <span style="color:#ae81ff">1265</span> (<span style="color:#ae81ff">01000</span>): Data truncated <span style="color:#66d9ef">for</span> <span style="color:#66d9ef">column</span> <span style="color:#e6db74">&#39;hobby&#39;</span> at row <span style="color:#ae81ff">1</span>
</span></span></code></pre></div><p>每个集合的元素都分配一个固定的数字，分配的方式从左往右按2的0、1、2、…次方</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032254622.png" alt="" /></p>
<hr>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">思考：已知集合占用</span><span style="color:#ae81ff">8</span><span style="color:#960050;background-color:#1e0010">个字节，最多可以表示几个选项？</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">答：</span><span style="color:#ae81ff">8</span><span style="color:#960050;background-color:#1e0010">个字节</span><span style="color:#f92672">=</span><span style="color:#ae81ff">64</span><span style="color:#960050;background-color:#1e0010">位，一个位表示</span><span style="color:#ae81ff">1</span><span style="color:#960050;background-color:#1e0010">个选项，最多可以表示</span><span style="color:#ae81ff">64</span><span style="color:#960050;background-color:#1e0010">个选项。</span>
</span></span></code></pre></div><h3 id="日期类型">
  日期类型
  <a class="anchor" href="#%e6%97%a5%e6%9c%9f%e7%b1%bb%e5%9e%8b">#</a>
</h3>
<table>
<thead>
<tr>
<th style="text-align:center">数据类型</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">datetime</td>
<td style="text-align:center">日期时间，占用8个字节</td>
</tr>
<tr>
<td style="text-align:center">date</td>
<td style="text-align:center">日期 占用3个字节</td>
</tr>
<tr>
<td style="text-align:center">time</td>
<td style="text-align:center">时间 占用3个字节</td>
</tr>
<tr>
<td style="text-align:center">timestamp</td>
<td style="text-align:center">时间戳，占用4个字节</td>
</tr>
<tr>
<td style="text-align:center">year</td>
<td style="text-align:center">年份   占用1个字节</td>
</tr>
</tbody>
</table>
<p>1、datetime   格式：年-月-日 小时:分钟:秒</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t10</span>(
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> field <span style="color:#66d9ef">datetime</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t10 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;2025-10-12 10:12:36&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t10 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;100-10-12 10:12:36&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t10 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;10000-10-12 10:12:36&#39;</span>);  <span style="color:#75715e">#datetime保存范围是：1~9999年
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>ERROR <span style="color:#ae81ff">1292</span> (<span style="color:#ae81ff">22007</span>): Incorrect <span style="color:#66d9ef">datetime</span> value: <span style="color:#e6db74">&#39;10000-10-12 10:12:36&#39;</span> <span style="color:#66d9ef">for</span> <span style="color:#66d9ef">column</span> <span style="color:#e6db74">&#39;field&#39;</span> at row <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t10;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> field               <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">2025</span><span style="color:#f92672">-</span><span style="color:#ae81ff">10</span><span style="color:#f92672">-</span><span style="color:#ae81ff">12</span> <span style="color:#ae81ff">10</span>:<span style="color:#ae81ff">12</span>:<span style="color:#ae81ff">36</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">0100</span><span style="color:#f92672">-</span><span style="color:#ae81ff">10</span><span style="color:#f92672">-</span><span style="color:#ae81ff">12</span> <span style="color:#ae81ff">10</span>:<span style="color:#ae81ff">12</span>:<span style="color:#ae81ff">36</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>2、date 日期格式</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t11</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> field <span style="color:#66d9ef">date</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t11 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;2025-10-12&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t11;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> field      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">2025</span><span style="color:#f92672">-</span><span style="color:#ae81ff">10</span><span style="color:#f92672">-</span><span style="color:#ae81ff">12</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span></code></pre></div><p>3、timestamp：时间戳</p>
<p>timestamp类型和 datetime类型在表现上是一样的。他们的区别：
datetime是从1到9999，而timestamp从1970年~2038年，2038年01月19日11:14:07秒以后就超出timestamp范围了。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t12</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> field <span style="color:#66d9ef">timestamp</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t12 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;1975-5-5 12:12:12&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t12 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;1969-5-5 12:12:12&#39;</span>);   <span style="color:#75715e"># 超出范围
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>ERROR <span style="color:#ae81ff">1292</span> (<span style="color:#ae81ff">22007</span>): Incorrect <span style="color:#66d9ef">datetime</span> value: <span style="color:#e6db74">&#39;1969-5-5 12:12:12&#39;</span> <span style="color:#66d9ef">for</span> <span style="color:#66d9ef">column</span> <span style="color:#e6db74">&#39;field&#39;</span> at row <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t12 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;2038-1-19 11:14:07&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t12 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;2038-1-19 11:14:08&#39;</span>);  <span style="color:#75715e"># 超出范围
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>ERROR <span style="color:#ae81ff">1292</span> (<span style="color:#ae81ff">22007</span>): Incorrect <span style="color:#66d9ef">datetime</span> value: <span style="color:#e6db74">&#39;2038-1-19 11:14:08&#39;</span> <span style="color:#66d9ef">for</span> <span style="color:#66d9ef">column</span> <span style="color:#e6db74">&#39;field&#39;</span> at row <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t12;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> field               <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">1975</span><span style="color:#f92672">-</span><span style="color:#ae81ff">05</span><span style="color:#f92672">-</span><span style="color:#ae81ff">05</span> <span style="color:#ae81ff">12</span>:<span style="color:#ae81ff">12</span>:<span style="color:#ae81ff">12</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">2038</span><span style="color:#f92672">-</span><span style="color:#ae81ff">01</span><span style="color:#f92672">-</span><span style="color:#ae81ff">19</span> <span style="color:#ae81ff">11</span>:<span style="color:#ae81ff">14</span>:<span style="color:#ae81ff">07</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+</span>
</span></span></code></pre></div><p>4、year</p>
<p>因为只占用1个字节，最多只能表示255个年份，范围是1901-2155之间的年份</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t13</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> field <span style="color:#66d9ef">year</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t13 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">2025</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t13 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1900</span>);   <span style="color:#75715e"># 超出范围
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>ERROR <span style="color:#ae81ff">1264</span> (<span style="color:#ae81ff">22003</span>): <span style="color:#66d9ef">Out</span> of range value <span style="color:#66d9ef">for</span> <span style="color:#66d9ef">column</span> <span style="color:#e6db74">&#39;field&#39;</span> at row <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t13 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">2155</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t13 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">2156</span>);   <span style="color:#75715e"># 超出范围
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>ERROR <span style="color:#ae81ff">1264</span> (<span style="color:#ae81ff">22003</span>): <span style="color:#66d9ef">Out</span> of range value <span style="color:#66d9ef">for</span> <span style="color:#66d9ef">column</span> <span style="color:#e6db74">&#39;field&#39;</span> at row <span style="color:#ae81ff">1</span>
</span></span></code></pre></div><p>5、time    表示时间或时间间隔，范围是-838:59:59~838:59:59</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t14</span>(
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> field <span style="color:#66d9ef">time</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t14 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;12:12:12&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t14 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;212:12:12&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t14 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;838:59:59&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t14 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;839:00:00&#39;</span>);  <span style="color:#75715e"># 操作范围
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>ERROR <span style="color:#ae81ff">1292</span> (<span style="color:#ae81ff">22007</span>): Incorrect <span style="color:#66d9ef">time</span> value: <span style="color:#e6db74">&#39;839:00:00&#39;</span> <span style="color:#66d9ef">for</span> <span style="color:#66d9ef">column</span> <span style="color:#e6db74">&#39;field&#39;</span> at row <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span>
</span></span></code></pre></div><p>多学一招：time支持以天的方式插入</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t14 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;10 10:10:10&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t14;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> field     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">12</span>:<span style="color:#ae81ff">12</span>:<span style="color:#ae81ff">12</span>  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">212</span>:<span style="color:#ae81ff">12</span>:<span style="color:#ae81ff">12</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">838</span>:<span style="color:#ae81ff">59</span>:<span style="color:#ae81ff">59</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">250</span>:<span style="color:#ae81ff">10</span>:<span style="color:#ae81ff">10</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------+</span>
</span></span></code></pre></div><h3 id="boolean">
  boolean
  <a class="anchor" href="#boolean">#</a>
</h3>
<p>MySQL不支持boolean类型，true和false在数据库中对应1和0。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t15</span>(
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> field boolean
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t15 <span style="color:#66d9ef">values</span> (<span style="color:#66d9ef">true</span>),(<span style="color:#66d9ef">false</span>);   <span style="color:#75715e"># true和false在数据库中对应1和0
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">2</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">2</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t15;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> field <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>     <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>     <span style="color:#ae81ff">0</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h2 id="列属性">
  列属性
  <a class="anchor" href="#%e5%88%97%e5%b1%9e%e6%80%a7">#</a>
</h2>
<h3 id="null--not-null">
  (null | not null)
  <a class="anchor" href="#null--not-null">#</a>
</h3>
<p>null：可以为空</p>
<p>not null：不可以为空</p>
<p>思考题</p>
<ol>
<li>
<p>学员姓名允许为空吗?                  非空</p>
</li>
<li>
<p>家庭地址允许为空吗?                  非空</p>
</li>
<li>
<p>电子邮件信息允许为空吗?           可以为空</p>
</li>
<li>
<p>考试成绩允许为空吗?                  可以为空</p>
</li>
</ol>
<h3 id="默认值default">
  默认值default
  <a class="anchor" href="#%e9%bb%98%e8%ae%a4%e5%80%bcdefault">#</a>
</h3>
<p>1、如果一个字段没有插入值，可以默认插入一个指定的值。</p>
<p>2、default关键字用来插入默认值</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t16</span>(
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span> <span style="color:#66d9ef">unsigned</span>,
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> addr <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span> <span style="color:#66d9ef">default</span> <span style="color:#e6db74">&#39;地址不详&#39;</span>
</span></span><span style="display:flex;"><span>   <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t16 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1</span>,<span style="color:#e6db74">&#39;北京&#39;</span>),(<span style="color:#ae81ff">2</span>,<span style="color:#66d9ef">default</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">2</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">2</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t16;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id   <span style="color:#f92672">|</span> addr     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">地址不详</span>   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="自动增长">
  自动增长
  <a class="anchor" href="#%e8%87%aa%e5%8a%a8%e5%a2%9e%e9%95%bf">#</a>
</h3>
<p>1、字段的值从1开始，每次递增1，特点就在字段中的数据不可能重复，适合为记录生成唯一的id</p>
<p>2、自动增长都是无符号整数。</p>
<p>3、在MySQL中，auto_increment必须是主键。但是主键不一定是自动增长的。</p>
<p>4、如果要给自动增长列插入数据，使用null关键字。</p>
<p>5、自动增长列上的数据被删除，默认情况下此记录的编号不再使用。</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032255047.png" alt="" /></p>
<h3 id="主键">
  主键
  <a class="anchor" href="#%e4%b8%bb%e9%94%ae">#</a>
</h3>
<p>主键：唯一标识表中记录的一个或一组列</p>
<p>主键的特点：不能重复，不能为空</p>
<p>一个表只能有一个主键，主键可以有多个字段组成。</p>
<p>主键的作用：</p>
<p>1、     保证数据完整性</p>
<p>2、     加快查询速度</p>
<h4 id="添加主键">
  添加主键
  <a class="anchor" href="#%e6%b7%bb%e5%8a%a0%e4%b8%bb%e9%94%ae">#</a>
</h4>
<p>方法一：创建表的时候添加主键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t17</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">5</span>) <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,   <span style="color:#75715e"># 创建主键
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>) <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t17 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;s2531&#39;</span>,<span style="color:#e6db74">&#39;tom&#39;</span>),(<span style="color:#e6db74">&#39;s2532&#39;</span>,<span style="color:#e6db74">&#39;berry&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">2</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">2</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t17;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s2531 <span style="color:#f92672">|</span> tom   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s2532 <span style="color:#f92672">|</span> berry <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 如果插入主键相同数据会报错
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t17 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;s2531&#39;</span>,<span style="color:#e6db74">&#39;tom&#39;</span>);
</span></span><span style="display:flex;"><span>ERROR <span style="color:#ae81ff">1062</span> (<span style="color:#ae81ff">23000</span>): Duplicate entry <span style="color:#e6db74">&#39;s2531&#39;</span> <span style="color:#66d9ef">for</span> <span style="color:#66d9ef">key</span> <span style="color:#e6db74">&#39;PRIMARY&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 主键不能插入null值
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t17 <span style="color:#66d9ef">values</span> (<span style="color:#66d9ef">null</span>,<span style="color:#e6db74">&#39;tom&#39;</span>);
</span></span><span style="display:flex;"><span>ERROR <span style="color:#ae81ff">1048</span> (<span style="color:#ae81ff">23000</span>): <span style="color:#66d9ef">Column</span> <span style="color:#e6db74">&#39;id&#39;</span> cannot be <span style="color:#66d9ef">null</span>
</span></span></code></pre></div><p>方法二：创建表的时候添加主键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t18</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>),
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>(id)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> t18;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type        <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span> <span style="color:#f92672">|</span> Extra <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)     <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span> PRI <span style="color:#f92672">|</span> <span style="color:#ae81ff">0</span>       <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>) <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>方法三：更改表的时候添加主键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t20</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> t20 <span style="color:#66d9ef">add</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span> (id);   <span style="color:#75715e"># 更改表添加主键
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">08</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> t20;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field <span style="color:#f92672">|</span> Type        <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span> <span style="color:#f92672">|</span> Extra <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id    <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)     <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span> PRI <span style="color:#f92672">|</span> <span style="color:#ae81ff">0</span>       <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>) <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="创建组合键">
  创建组合键
  <a class="anchor" href="#%e5%88%9b%e5%bb%ba%e7%bb%84%e5%90%88%e9%94%ae">#</a>
</h4>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032255599.png" alt="" /></p>
<h4 id="查看主键">
  查看主键
  <a class="anchor" href="#%e6%9f%a5%e7%9c%8b%e4%b8%bb%e9%94%ae">#</a>
</h4>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032255080.png" alt="" /></p>
<h4 id="删除主键">
  删除主键
  <a class="anchor" href="#%e5%88%a0%e9%99%a4%e4%b8%bb%e9%94%ae">#</a>
</h4>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032256720.png" alt="" /></p>
<h4 id="选择主键的原则">
  选择主键的原则
  <a class="anchor" href="#%e9%80%89%e6%8b%a9%e4%b8%bb%e9%94%ae%e7%9a%84%e5%8e%9f%e5%88%99">#</a>
</h4>
<p>1、     最少性：尽量选择一个字段做主键</p>
<p>2、     稳定性：尽量选择更新少的列做主键</p>
<p>3、     尽量选择数字型的列做主键</p>
<h4 id="主键思考题">
  主键思考题
  <a class="anchor" href="#%e4%b8%bb%e9%94%ae%e6%80%9d%e8%80%83%e9%a2%98">#</a>
</h4>
<p>1、在主键列输入的数值，允许为空吗?       不可以</p>
<p>2、 一个表可以有多个主键吗?                     不可以</p>
<p>3、 在一个学校数据库中，如果一个学校内允许重名的学员，但是一个班级内不允许学员重名，可以组合班级和姓名两个字段一起来作为主键吗？                  可以</p>
<p>4、 标识列（自动增长列）允许为字符数据类型吗？     不可以</p>
<p>5、 表中没有合适的列作为主键怎么办？      添加自动增加列</p>
<p>6、  如果标识列A的初始值为1，增长量为1，则输入三行数据以后，再删除两行，下次再输入数据行的时候，标识值从多少开始？          从4开始</p>
<h3 id="唯一键">
  唯一键
  <a class="anchor" href="#%e5%94%af%e4%b8%80%e9%94%ae">#</a>
</h3>
<p>特点：</p>
<p>1、不能重复，可以为空</p>
<p>2、一个表可以有多个唯一键</p>
<p>作用：</p>
<p>1、     保证数据不能重复。保证数据完整性</p>
<p>2、     加快数据访问</p>
<h4 id="添加唯一键">
  添加唯一键
  <a class="anchor" href="#%e6%b7%bb%e5%8a%a0%e5%94%af%e4%b8%80%e9%94%ae">#</a>
</h4>
<p>方法一：创建表的时候添加唯一键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t22</span>(   
</span></span><span style="display:flex;"><span> <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,    
</span></span><span style="display:flex;"><span> <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">unique</span>,    <span style="color:#75715e">#通过unique添加唯一键    
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> <span style="color:#f92672">-&gt;</span> addr <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">100</span>) <span style="color:#66d9ef">unique</span>    
</span></span><span style="display:flex;"><span> <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span> Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span> mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t22 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1</span>,<span style="color:#e6db74">&#39;tom&#39;</span>,<span style="color:#e6db74">&#39;上海&#39;</span>);
</span></span><span style="display:flex;"><span> Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">05</span> sec)
</span></span><span style="display:flex;"><span> mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t22 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">2</span>,<span style="color:#e6db74">&#39;tom&#39;</span>,<span style="color:#e6db74">&#39;北京&#39;</span>);    <span style="color:#75715e">#  name重复了，报错ERROR 1062 (23000): Duplicate entry &#39;tom&#39; for key &#39;name&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t22 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">2</span>,<span style="color:#e6db74">&#39;berry&#39;</span>,<span style="color:#e6db74">&#39;上海&#39;</span>);   <span style="color:#75715e"># addr重复了 ERROR 1062 (23000): Duplicate entry &#39;上海&#39; for key &#39;addr&#39;
</span></span></span></code></pre></div><p>还有一种方法</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t26</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>),
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> addr <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>),
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>(id),
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">unique</span> (name),     <span style="color:#75715e"># 添加唯一键
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">unique</span> (addr)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span></code></pre></div><p>方法二：修改表的时候添加唯一键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t23</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> t23 <span style="color:#66d9ef">add</span> <span style="color:#66d9ef">unique</span> (name);    <span style="color:#75715e">#  添加一个唯一键
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><p>一次添加多个唯一键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t24</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>),
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> addr <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> t24 <span style="color:#66d9ef">add</span> <span style="color:#66d9ef">unique</span>(name),<span style="color:#66d9ef">add</span> <span style="color:#66d9ef">unique</span>(addr);  
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">09</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><p>添加组合唯一键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t25</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>),
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> addr <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">09</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> t25 <span style="color:#66d9ef">add</span> <span style="color:#66d9ef">unique</span>(name,addr);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">01</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h4 id="查看唯一键">
  查看唯一键
  <a class="anchor" href="#%e6%9f%a5%e7%9c%8b%e5%94%af%e4%b8%80%e9%94%ae">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> t26<span style="color:#960050;background-color:#1e0010">\</span>G
</span></span><span style="display:flex;"><span><span style="color:#f92672">***************************</span> <span style="color:#ae81ff">1</span>. row <span style="color:#f92672">***************************</span>
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">Table</span>: t26
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">Create</span> <span style="color:#66d9ef">Table</span>: <span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">TABLE</span> <span style="color:#f92672">`</span>t26<span style="color:#f92672">`</span> (
</span></span><span style="display:flex;"><span> <span style="color:#f92672">`</span>id<span style="color:#f92672">`</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>) <span style="color:#66d9ef">NOT</span> <span style="color:#66d9ef">NULL</span> <span style="color:#66d9ef">DEFAULT</span> <span style="color:#e6db74">&#39;0&#39;</span>,
</span></span><span style="display:flex;"><span> <span style="color:#f92672">`</span>name<span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">NULL</span>,
</span></span><span style="display:flex;"><span> <span style="color:#f92672">`</span>addr<span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">NULL</span>,
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">PRIMARY</span> <span style="color:#66d9ef">KEY</span> (<span style="color:#f92672">`</span>id<span style="color:#f92672">`</span>),
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">UNIQUE</span> <span style="color:#66d9ef">KEY</span> <span style="color:#f92672">`</span>name<span style="color:#f92672">`</span> (<span style="color:#f92672">`</span>name<span style="color:#f92672">`</span>),      <span style="color:#75715e"># 唯一键
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> <span style="color:#66d9ef">UNIQUE</span> <span style="color:#66d9ef">KEY</span> <span style="color:#f92672">`</span>addr<span style="color:#f92672">`</span> (<span style="color:#f92672">`</span>addr<span style="color:#f92672">`</span>)       <span style="color:#75715e"># 唯一键
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>) <span style="color:#66d9ef">ENGINE</span><span style="color:#f92672">=</span>InnoDB <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">CHARSET</span><span style="color:#f92672">=</span>utf8
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> t25<span style="color:#960050;background-color:#1e0010">\</span>G
</span></span><span style="display:flex;"><span><span style="color:#f92672">***************************</span> <span style="color:#ae81ff">1</span>. row <span style="color:#f92672">***************************</span>
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">Table</span>: t25
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">Create</span> <span style="color:#66d9ef">Table</span>: <span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">TABLE</span> <span style="color:#f92672">`</span>t25<span style="color:#f92672">`</span> (
</span></span><span style="display:flex;"><span> <span style="color:#f92672">`</span>id<span style="color:#f92672">`</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>) <span style="color:#66d9ef">NOT</span> <span style="color:#66d9ef">NULL</span>,
</span></span><span style="display:flex;"><span> <span style="color:#f92672">`</span>name<span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">NULL</span>,
</span></span><span style="display:flex;"><span> <span style="color:#f92672">`</span>addr<span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">NULL</span>,
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">PRIMARY</span> <span style="color:#66d9ef">KEY</span> (<span style="color:#f92672">`</span>id<span style="color:#f92672">`</span>),
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">UNIQUE</span> <span style="color:#66d9ef">KEY</span> <span style="color:#f92672">`</span>name<span style="color:#f92672">`</span> (<span style="color:#f92672">`</span>name<span style="color:#f92672">`</span>,<span style="color:#f92672">`</span>addr<span style="color:#f92672">`</span>)    <span style="color:#75715e"># 组合唯一键
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>) <span style="color:#66d9ef">ENGINE</span><span style="color:#f92672">=</span>InnoDB <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">CHARSET</span><span style="color:#f92672">=</span>utf8
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>添加唯一键，给唯一键取名</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t27</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">03</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> t27 <span style="color:#66d9ef">add</span> <span style="color:#66d9ef">unique</span> <span style="color:#a6e22e">UQ_name</span>(name);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> t27<span style="color:#960050;background-color:#1e0010">\</span>G
</span></span><span style="display:flex;"><span><span style="color:#f92672">***************************</span> <span style="color:#ae81ff">1</span>. row <span style="color:#f92672">***************************</span>
</span></span><span style="display:flex;"><span>       <span style="color:#66d9ef">Table</span>: t27
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">Create</span> <span style="color:#66d9ef">Table</span>: <span style="color:#66d9ef">CREATE</span> <span style="color:#66d9ef">TABLE</span> <span style="color:#f92672">`</span>t27<span style="color:#f92672">`</span> (
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">`</span>name<span style="color:#f92672">`</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>) <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">NULL</span>,
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">UNIQUE</span> <span style="color:#66d9ef">KEY</span> <span style="color:#f92672">`</span>UQ_name<span style="color:#f92672">`</span> (<span style="color:#f92672">`</span>name<span style="color:#f92672">`</span>)    <span style="color:#75715e"># 唯一键的名字是UQ_name
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>) <span style="color:#66d9ef">ENGINE</span><span style="color:#f92672">=</span>InnoDB <span style="color:#66d9ef">DEFAULT</span> <span style="color:#66d9ef">CHARSET</span><span style="color:#f92672">=</span>utf8
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="删除唯一键">
  删除唯一键
  <a class="anchor" href="#%e5%88%a0%e9%99%a4%e5%94%af%e4%b8%80%e9%94%ae">#</a>
</h4>
<p>通过唯一键的名字来删除唯一键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> <span style="color:#960050;background-color:#1e0010">表名</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">index</span> <span style="color:#960050;background-color:#1e0010">唯一键名称</span>
</span></span></code></pre></div><p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032256891.png" alt="" /></p>
<p>问题：主键和唯一键的区别？</p>
<p>1、主键不能重复，不能为空，唯一键不能重复，可以为空</p>
<p>2、主键只有一个，唯一键可以有多个。</p>
<h3 id="备注comment">
  备注comment
  <a class="anchor" href="#%e5%a4%87%e6%b3%a8comment">#</a>
</h3>
<p>为了程序员之间的相互交流  <img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032257266.png" alt="" /></p>
<h4 id="sql注释">
  SQL注释
  <a class="anchor" href="#sql%e6%b3%a8%e9%87%8a">#</a>
</h4>
<p>单行注释：&ndash;或#</p>
<p>多行注释：/*    */</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032257168.png" alt="" /></p>
<h2 id="数据完整性介绍">
  数据完整性介绍
  <a class="anchor" href="#%e6%95%b0%e6%8d%ae%e5%ae%8c%e6%95%b4%e6%80%a7%e4%bb%8b%e7%bb%8d">#</a>
</h2>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032257159.png" alt="" /></p>
<h4 id="保证实体完整性">
  保证实体完整性
  <a class="anchor" href="#%e4%bf%9d%e8%af%81%e5%ae%9e%e4%bd%93%e5%ae%8c%e6%95%b4%e6%80%a7">#</a>
</h4>
<p>1、     主键约束</p>
<p>2、     唯一约束</p>
<p>3、     自动增长列</p>
<h4 id="保证域完整性">
  保证域完整性
  <a class="anchor" href="#%e4%bf%9d%e8%af%81%e5%9f%9f%e5%ae%8c%e6%95%b4%e6%80%a7">#</a>
</h4>
<p>1、     数据类型约束</p>
<p>2、     非空约束</p>
<p>3、     默认值约束</p>
<h4 id="保证引用完整性">
  保证引用完整性
  <a class="anchor" href="#%e4%bf%9d%e8%af%81%e5%bc%95%e7%94%a8%e5%ae%8c%e6%95%b4%e6%80%a7">#</a>
</h4>
<p>1、外键约束：从表中的公共字段是主表的外键</p>
<h2 id="引用完整性">
  引用完整性
  <a class="anchor" href="#%e5%bc%95%e7%94%a8%e5%ae%8c%e6%95%b4%e6%80%a7">#</a>
</h2>
<h4 id="主表和从表">
  主表和从表
  <a class="anchor" href="#%e4%b8%bb%e8%a1%a8%e5%92%8c%e4%bb%8e%e8%a1%a8">#</a>
</h4>
<p>两个表建立关系（两个表只要有公共字段就有关系），一个表称为主表，一个表称为从表。</p>
<p>外键约束可以实现：</p>
<p>1、     主表中没有的从表中不允许插入</p>
<p>2、     从表中有的主表中不允许删除</p>
<p>3、     不能更改主表中的值而导致从表中的记录孤立存在。</p>
<p>4、     先删除从表，再删除主表</p>
<h4 id="外键foreign-key">
  外键（foreign key）
  <a class="anchor" href="#%e5%a4%96%e9%94%aeforeign-key">#</a>
</h4>
<p>1、     外键：从表中的公共字段，公共字段的名字可以不一样，但是数据类型必须一样。</p>
<p>2、     外键约束用来保证引用完整性</p>
<h4 id="添加外键">
  添加外键
  <a class="anchor" href="#%e6%b7%bb%e5%8a%a0%e5%a4%96%e9%94%ae">#</a>
</h4>
<p>方法一：创建表的时候添加外键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stuinfo</span>(
</span></span><span style="display:flex;"><span>   stuno <span style="color:#66d9ef">char</span>(<span style="color:#ae81ff">4</span>) <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>   name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>) <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span>
</span></span><span style="display:flex;"><span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stumarks</span>(
</span></span><span style="display:flex;"><span>   stuid <span style="color:#66d9ef">char</span>(<span style="color:#ae81ff">4</span>) <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>   score <span style="color:#66d9ef">tinyint</span> <span style="color:#66d9ef">unsigned</span>,
</span></span><span style="display:flex;"><span>   <span style="color:#66d9ef">foreign</span> <span style="color:#66d9ef">key</span> (stuid) <span style="color:#66d9ef">references</span> <span style="color:#a6e22e">stuinfo</span>(stuno)
</span></span><span style="display:flex;"><span>);
</span></span></code></pre></div><p>方法二：修改表的时候添加外键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stuinfo</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span>  stuno <span style="color:#66d9ef">char</span>(<span style="color:#ae81ff">4</span>) <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span>  name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>) <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stumarks</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span>  stuid <span style="color:#66d9ef">char</span>(<span style="color:#ae81ff">4</span>) <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span>  score <span style="color:#66d9ef">tinyint</span> <span style="color:#66d9ef">unsigned</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span>  <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> <span style="color:#960050;background-color:#1e0010">从表</span> <span style="color:#66d9ef">add</span> <span style="color:#66d9ef">foreign</span> <span style="color:#66d9ef">key</span> (<span style="color:#960050;background-color:#1e0010">从表的公共字段</span>) <span style="color:#66d9ef">references</span> <span style="color:#960050;background-color:#1e0010">主表</span>(<span style="color:#960050;background-color:#1e0010">公共字段</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> stumarks <span style="color:#66d9ef">add</span> <span style="color:#66d9ef">foreign</span> <span style="color:#66d9ef">key</span> (stuid) <span style="color:#66d9ef">references</span> <span style="color:#a6e22e">stuinfo</span>(stuno);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><p>脚下留心：要创建外键必须是innodb引擎，myisam不支持外键约束</p>
<h4 id="查看外键">
  查看外键
  <a class="anchor" href="#%e6%9f%a5%e7%9c%8b%e5%a4%96%e9%94%ae">#</a>
</h4>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032257803.png" alt="" /></p>
<h4 id="删除外键">
  删除外键
  <a class="anchor" href="#%e5%88%a0%e9%99%a4%e5%a4%96%e9%94%ae">#</a>
</h4>
<p>通过外键的名字删除外键</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> <span style="color:#960050;background-color:#1e0010">表名</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">foreign</span> <span style="color:#66d9ef">key</span> <span style="color:#960050;background-color:#1e0010">外键名</span>  <span style="color:#f92672">//</span><span style="color:#960050;background-color:#1e0010">外键名是上面查看出来的</span>
</span></span></code></pre></div><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> stumarks <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">foreign</span> <span style="color:#66d9ef">key</span> stumarks_ibfk_1;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h3 id="外键操作">
  外键操作
  <a class="anchor" href="#%e5%a4%96%e9%94%ae%e6%93%8d%e4%bd%9c">#</a>
</h3>
<p>1、     严格操作（前面讲的是严格操作）</p>
<p>2、     置空操作（set null）：如果主表记录删除或更新，从表置空</p>
<p>3、     级联操作（cascade）：如果主表记录删除或更新，从表级联</p>
<p>一般来说：主表删除的时候，从表置空操作，主表更新的时候，从表级联操作。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">foreign</span> <span style="color:#66d9ef">key</span>(<span style="color:#960050;background-color:#1e0010">外键</span>) <span style="color:#66d9ef">references</span> <span style="color:#960050;background-color:#1e0010">主表</span>(<span style="color:#960050;background-color:#1e0010">关键字段</span>)[<span style="color:#960050;background-color:#1e0010">主表删除是的动作</span>][<span style="color:#960050;background-color:#1e0010">主表更新时候的动作</span>]
</span></span></code></pre></div><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stuinfo</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> stuno <span style="color:#66d9ef">char</span>(<span style="color:#ae81ff">4</span>) <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>) <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stumarks</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> stuid <span style="color:#66d9ef">int</span> <span style="color:#66d9ef">auto_increment</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> stuno <span style="color:#66d9ef">char</span>(<span style="color:#ae81ff">4</span>) ,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> score <span style="color:#66d9ef">tinyint</span> <span style="color:#66d9ef">unsigned</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span>  <span style="color:#66d9ef">foreign</span> <span style="color:#66d9ef">key</span> (stuno) <span style="color:#66d9ef">references</span> <span style="color:#a6e22e">stuinfo</span>(stuno) <span style="color:#66d9ef">on</span> <span style="color:#66d9ef">delete</span> <span style="color:#66d9ef">set</span> <span style="color:#66d9ef">null</span> <span style="color:#66d9ef">on</span> <span style="color:#66d9ef">update</span> <span style="color:#66d9ef">cascade</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> stuinfo <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;s101&#39;</span>,<span style="color:#e6db74">&#39;tom&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> stumarks <span style="color:#66d9ef">values</span> (<span style="color:#66d9ef">null</span>,<span style="color:#e6db74">&#39;s101&#39;</span>,<span style="color:#ae81ff">88</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuno <span style="color:#f92672">|</span> name <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s101  <span style="color:#f92672">|</span> tom  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">update</span> stuinfo <span style="color:#66d9ef">set</span> stuno<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;s102&#39;</span> <span style="color:#66d9ef">where</span> stuno<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;s101&#39;</span>;   <span style="color:#75715e"># 更新时级联
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Rows matched: <span style="color:#ae81ff">1</span>  Changed: <span style="color:#ae81ff">1</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stumarks;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuid <span style="color:#f92672">|</span> stuno <span style="color:#f92672">|</span> score <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>     <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> s102  <span style="color:#f92672">|</span>    <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">delete</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> stuno<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;s102&#39;</span>;    <span style="color:#75715e"># 删除时置空
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stumarks;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuid <span style="color:#f92672">|</span> stuno <span style="color:#f92672">|</span> score <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>     <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>  <span style="color:#f92672">|</span>    <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+-------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h2 id="数据库基本概念">
  数据库基本概念
  <a class="anchor" href="#%e6%95%b0%e6%8d%ae%e5%ba%93%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5">#</a>
</h2>
<p>1、关系：两个表的公共字段</p>
<p>2、行：也称记录，也称实体</p>
<p>3、列：也称字段，也称属性</p>
<pre tabindex="0"><code>就表结构而言，表分为行和列；
就表数据而言，分为记录和字段；
就面向对象而言，一个记录就是一个实体，一个字段就是一个属性。
</code></pre><p>4、数据冗余：相同的数据存储在不同的地方</p>
<pre tabindex="0"><code>脚下留心：
1、冗余只能减少，不能杜绝。
2、减少冗余的方法是分表
3、为减少数据查找的麻烦，允许数据有一定的冗余
</code></pre><p>5、数据完整性：正确性+准确性=数据完整性</p>
<pre tabindex="0"><code>正确性：数据类型正确
准确性：数据范围要准确
</code></pre><h2 id="实体和实体之间的关系">
  实体和实体之间的关系
  <a class="anchor" href="#%e5%ae%9e%e4%bd%93%e5%92%8c%e5%ae%9e%e4%bd%93%e4%b9%8b%e9%97%b4%e7%9a%84%e5%85%b3%e7%b3%bb">#</a>
</h2>
<p>1、一对一</p>
<p>2、一对多 （多对一）</p>
<p>3、多对多 <img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032258141.png" alt="" /></p>
<h4 id="一对多-1n">
  一对多 1：N
  <a class="anchor" href="#%e4%b8%80%e5%af%b9%e5%a4%9a-1n">#</a>
</h4>
<p>1、主表中的一条记录对应从表中的多条记录。</p>
<p>2、一对多和多对一是一样的</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032258158.png" alt="" /></p>
<hr>
<p>如何实现一对多？</p>
<p>答：主键和非主键建关系</p>
<h4 id="一对一11">
  一对一（1:1）
  <a class="anchor" href="#%e4%b8%80%e5%af%b9%e4%b8%8011">#</a>
</h4>
<p>1、主表中的一条记录对应从表中的一条记录</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032258780.png" alt="" /></p>
<p>如何实现一对一？</p>
<p>主键和主键建关系就能实现一对一。</p>
<pre tabindex="0"><code>思考：一对一两个表完全可以用一个表实现，为什么还要分成两个表？

答：在字段数量很多情况下，数据量也就很大，每次查询都需要检索大量数据，这样效率低下。我们可以将所有字段分成两个部分，“常用字段”和“不常用字段”，这样对大部分查询者来说效率提高了。【表的垂直分割】
</code></pre><h4 id="多对多nm">
  多对多（N：M）
  <a class="anchor" href="#%e5%a4%9a%e5%af%b9%e5%a4%9anm">#</a>
</h4>
<p>主表中的一条记录对应从表中的多条记录，从表中的一条记录对应主表中的多条记录</p>
<p>班级和讲师的关系</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032259221.png" alt="" /></p>
<p>如何实现多对多？</p>
<p>答：建立第三张表来保存关系。</p>
<p>问题：说出几个多对多的关系？</p>
<p>1、科目表和学生表的关系		2、商品表和订单表         3、游戏目录表和玩家表</p>
<h2 id="数据库设计的步骤">
  数据库设计的步骤
  <a class="anchor" href="#%e6%95%b0%e6%8d%ae%e5%ba%93%e8%ae%be%e8%ae%a1%e7%9a%84%e6%ad%a5%e9%aa%a4">#</a>
</h2>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032259828.png" alt="" /></p>
<h4 id="具体步骤">
  具体步骤
  <a class="anchor" href="#%e5%85%b7%e4%bd%93%e6%ad%a5%e9%aa%a4">#</a>
</h4>
<p>1、     收集信息：与该系统有关人员进行交流、坐谈，充分理解数据库需要完成的任务</p>
<p>2、     标识对象（实体－Entity）标识数据库要管理的关键对象或实体</p>
<p>3、     标识每个实体的属性（Attribute）</p>
<p>4、     标识对象之间的关系（Relationship）</p>
<p>5、     将模型转换成数据库</p>
<p>6、     规范化</p>
<h4 id="绘制e-r图">
  绘制E-R图
  <a class="anchor" href="#%e7%bb%98%e5%88%b6e-r%e5%9b%be">#</a>
</h4>
<p>E-R（Entity－Relationship）实体关系图</p>
<p>E-R图的语法</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032259296.png" alt="" /></p>
<p>绘制E-R图</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032300960.png" alt="" /></p>
<h4 id="将e-r图转成表">
  将E-R图转成表
  <a class="anchor" href="#%e5%b0%86e-r%e5%9b%be%e8%bd%ac%e6%88%90%e8%a1%a8">#</a>
</h4>
<p>1、     实体转成表，属性转成字段</p>
<p>2、     如果没有合适的字段做主键，给表添加一个自动增长列做主键。</p>
<h4 id="例题">
  例题
  <a class="anchor" href="#%e4%be%8b%e9%a2%98">#</a>
</h4>
<p>1、项目需求</p>
<pre tabindex="0"><code>BBS论坛的基本功能：
用户注册和登录，后台数据库需要存放用户的注册信息和在线状态信息；
用户发贴，后台数据库需要存放贴子相关信息，如贴子内容、标题等；
用户可以对发帖进行回复；
论坛版块管理：后台数据库需要存放各个版块信息，如版主、版块名称、贴子数等；
</code></pre><p>2、标识对象</p>
<pre><code>参与的对象有：用户、发的帖子、跟帖、板块
</code></pre>
<p>3、标识对象的属性 <img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032300295.png" alt="" /></p>
<p>4、建立关系，绘制E-R图</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032301271.png" alt="" /></p>
<p>5、将E-R图转出表结构</p>
<h2 id="数据规范化">
  数据规范化
  <a class="anchor" href="#%e6%95%b0%e6%8d%ae%e8%a7%84%e8%8c%83%e5%8c%96">#</a>
</h2>
<p>Codd博士定义了6个范式来规范化数据库，范式由小到大来约束，范式越高冗余越小，但表的个数也越多。实验证明，三范式是性价比最高的。</p>
<h4 id="第一范式确保每列原子性">
  第一范式：确保每列原子性
  <a class="anchor" href="#%e7%ac%ac%e4%b8%80%e8%8c%83%e5%bc%8f%e7%a1%ae%e4%bf%9d%e6%af%8f%e5%88%97%e5%8e%9f%e5%ad%90%e6%80%a7">#</a>
</h4>
<p>第一范式确保每个字段不可再分</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032301260.png" alt="" /></p>
<p>思考：如下表设计是否合理？</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032301449.png" alt="" /></p>
<p>不合理。不满足第一范式，上课时间可以再分</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032302111.png" alt="" /></p>
<p>思考：地址包含省、市、县、地区是否需要拆分？</p>
<p>答：如果仅仅起地址的作用，不需要统计，可以不拆分；如果有按地区统计的功能需要拆分。</p>
<p>在实际项目中，建议拆分。</p>
<h4 id="第二范式非键字段必须依赖于键字段">
  第二范式：非键字段必须依赖于键字段
  <a class="anchor" href="#%e7%ac%ac%e4%ba%8c%e8%8c%83%e5%bc%8f%e9%9d%9e%e9%94%ae%e5%ad%97%e6%ae%b5%e5%bf%85%e9%a1%bb%e4%be%9d%e8%b5%96%e4%ba%8e%e9%94%ae%e5%ad%97%e6%ae%b5">#</a>
</h4>
<p>一个表只能描述一件事</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032302187.png" alt="" /></p>
<p>思考：如下表设计是否合理？</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032302193.png" alt="" /></p>
<h4 id="第三范式消除传递依赖">
  第三范式：消除传递依赖
  <a class="anchor" href="#%e7%ac%ac%e4%b8%89%e8%8c%83%e5%bc%8f%e6%b6%88%e9%99%a4%e4%bc%a0%e9%80%92%e4%be%9d%e8%b5%96">#</a>
</h4>
<p>在所有的非键字段中，不能有传递依赖</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032303687.png" alt="" /></p>
<p>下列设计是否满足第三范式？</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032303423.png" alt="" /></p>
<p>不满足，因为语文和数学确定了，总分就确定了。</p>
<pre tabindex="0"><code>多学一招：上面的设计不满足第三范式，但是高考分数表就是这样设计的，为什么？

答：高考分数峰值访问量非常大，这时候就是性能更重要。当性能和规范化冲突的时候，我们首选性能。这就是“反三范式”。
</code></pre><h4 id="数据库设计的例题">
  数据库设计的例题
  <a class="anchor" href="#%e6%95%b0%e6%8d%ae%e5%ba%93%e8%ae%be%e8%ae%a1%e7%9a%84%e4%be%8b%e9%a2%98">#</a>
</h4>
<p>1、需求</p>
<pre tabindex="0"><code>公司承担多个工程项目，每一项工程有：工程号、工程名称、施工人员等
公司有多名职工，每一名职工有：职工号、姓名、性别、职务（工程师、技术员）等
公司按照工时和小时工资率支付工资，小时工资率由职工的职务决定（例如，技术员的小时工资率与工程师不同）
</code></pre><p>2、工资表</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032303900.png" alt="" /></p>
<p>3、将工资表转成数据库表</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032303311.png" alt="" /></p>
<p>4、这个表存在的问题</p>
<pre><code>A：新人入职需要虚拟一个项目

B：职务更改，小时工资率可能会忘记更改，造成数据不完整

C：有人离职，删除记录后，工程也没有了
</code></pre>
<p>5、规范化表</p>
<pre><code>第一步：这个表满足第一范式

第二步：这个表不是描述了一件事情
</code></pre>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032304680.png" alt="" /></p>
<pre><code>第三步：是否满足第三范式
</code></pre>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032304892.png" alt="" /></p>
<p>更改如下：</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032305448.png" alt="" /></p>
<h2 id="查询语句">
  查询语句
  <a class="anchor" href="#%e6%9f%a5%e8%af%a2%e8%af%ad%e5%8f%a5">#</a>
</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">select</span> [<span style="color:#960050;background-color:#1e0010">选项</span>] <span style="color:#960050;background-color:#1e0010">列名</span> [<span style="color:#66d9ef">from</span> <span style="color:#960050;background-color:#1e0010">表名</span>] [<span style="color:#66d9ef">where</span> <span style="color:#960050;background-color:#1e0010">条件</span>]  [<span style="color:#66d9ef">group</span> <span style="color:#66d9ef">by</span> <span style="color:#960050;background-color:#1e0010">分组</span>] [<span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> <span style="color:#960050;background-color:#1e0010">排序</span>][<span style="color:#66d9ef">having</span> <span style="color:#960050;background-color:#1e0010">条件</span>] [<span style="color:#66d9ef">limit</span> <span style="color:#960050;background-color:#1e0010">限制</span>]
</span></span></code></pre></div><h3 id="字段表达式">
  字段表达式
  <a class="anchor" href="#%e5%ad%97%e6%ae%b5%e8%a1%a8%e8%be%be%e5%bc%8f">#</a>
</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#e6db74">&#39;锄禾日当午&#39;</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">锄禾日当午</span>          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">锄禾日当午</span>          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#ae81ff">10</span><span style="color:#f92672">*</span><span style="color:#ae81ff">10</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">10</span><span style="color:#f92672">*</span><span style="color:#ae81ff">10</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>   <span style="color:#ae81ff">100</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------+</span>
</span></span></code></pre></div><p>通过as给字段取别名</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span>  <span style="color:#66d9ef">select</span> <span style="color:#e6db74">&#39;锄禾日当午&#39;</span> <span style="color:#66d9ef">as</span> content;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> content    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">锄禾日当午</span>          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#ae81ff">10</span><span style="color:#f92672">*</span><span style="color:#ae81ff">10</span> <span style="color:#66d9ef">as</span> result;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> result <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">100</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>多学一招：as可以省略</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#ae81ff">10</span><span style="color:#f92672">*</span><span style="color:#ae81ff">10</span>  result;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> result <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">100</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="from子句">
  from子句
  <a class="anchor" href="#from%e5%ad%90%e5%8f%a5">#</a>
</h3>
<p>from：来自，from后面跟的是数据源。数据源可以有多个。返回笛卡尔积。</p>
<p>插入测试表</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t1</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">05</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t2</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> field1 <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>),
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> field2 <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t1 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1</span>,<span style="color:#e6db74">&#39;tom&#39;</span>),(<span style="color:#ae81ff">2</span>,<span style="color:#e6db74">&#39;berry&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">2</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">2</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t2 <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;333&#39;</span>,<span style="color:#e6db74">&#39;333&#39;</span>),(<span style="color:#e6db74">&#39;444&#39;</span>,<span style="color:#e6db74">&#39;444&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">2</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">2</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><p>测试多个数据源</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t1,t2;              <span style="color:#75715e"># 返回笛卡尔积
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+------+-------+--------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id   <span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> field1 <span style="color:#f92672">|</span> field2 <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+-------+--------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> tom   <span style="color:#f92672">|</span> <span style="color:#ae81ff">333</span>    <span style="color:#f92672">|</span> <span style="color:#ae81ff">333</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> berry <span style="color:#f92672">|</span> <span style="color:#ae81ff">333</span>    <span style="color:#f92672">|</span> <span style="color:#ae81ff">333</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> tom   <span style="color:#f92672">|</span> <span style="color:#ae81ff">444</span>    <span style="color:#f92672">|</span> <span style="color:#ae81ff">444</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> berry <span style="color:#f92672">|</span> <span style="color:#ae81ff">444</span>    <span style="color:#f92672">|</span> <span style="color:#ae81ff">444</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+-------+--------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">4</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="dual表">
  dual表
  <a class="anchor" href="#dual%e8%a1%a8">#</a>
</h3>
<p>dual表是一个伪表。在有些特定情况下，没有具体的表的参与，但是为了保证select语句的完整又必须要一个表名，这时候就使用伪表。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#ae81ff">10</span><span style="color:#f92672">*</span><span style="color:#ae81ff">10</span> <span style="color:#66d9ef">as</span> result <span style="color:#66d9ef">from</span> <span style="color:#66d9ef">dual</span>;  <span style="color:#75715e">#dual表是用来保证select语句的完整性。
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> result <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">100</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+</span>
</span></span></code></pre></div><h3 id="where子句">
  where子句
  <a class="anchor" href="#where%e5%ad%90%e5%8f%a5">#</a>
</h3>
<p>where后面跟的是条件，在数据源中进行筛选。返回条件为真记录</p>
<p>MySQL支持的运算符</p>
<ol>
<li><code>&gt;</code>	大于</li>
<li><code>&lt;</code>小于</li>
<li><code>&gt;=</code></li>
<li><code>&lt;=</code></li>
<li><code>=</code></li>
<li><code>!=</code></li>
<li>and    与</li>
<li>or      或</li>
<li>not   非</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stusex<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;男&#39;</span>;		<span style="color:#75715e"># 查找性别是男的记录
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuage<span style="color:#f92672">&gt;=</span><span style="color:#ae81ff">20</span>;		<span style="color:#75715e"># 查找年龄不低于20的记录
</span></span></span></code></pre></div><p>思考：如下代码输出什么</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> <span style="color:#ae81ff">1</span>      <span style="color:#75715e"># 返回所有数据库
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> <span style="color:#ae81ff">0</span>		<span style="color:#75715e">#返回空记录
</span></span></span></code></pre></div><p>思考：如何查找北京和上海的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuaddress<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;上海&#39;</span> <span style="color:#66d9ef">or</span> stuaddress<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;北京&#39;</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">76</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">55</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25320 <span style="color:#f92672">|</span> Tom     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">24</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">8</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">67</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span></code></pre></div><h3 id="in--not-in">
  in | not in
  <a class="anchor" href="#in--not-in">#</a>
</h3>
<p>上面的查询上海和北京的学生的SQL可以通过in语句来实现</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuaddress <span style="color:#66d9ef">in</span> (<span style="color:#e6db74">&#39;北京&#39;</span>,<span style="color:#e6db74">&#39;上海&#39;</span>);
</span></span></code></pre></div><p>练习：</p>
<p>1、查找学号是s25301,s25302,s25303的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuno <span style="color:#66d9ef">in</span> (<span style="color:#e6db74">&#39;s25301&#39;</span>,<span style="color:#e6db74">&#39;s25302&#39;</span>,<span style="color:#e6db74">&#39;s25303&#39;</span>);
</span></span></code></pre></div><p>2、查找年龄是18,19,20的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuage <span style="color:#66d9ef">in</span>(<span style="color:#ae81ff">18</span>,<span style="color:#ae81ff">19</span>,<span style="color:#ae81ff">20</span>);
</span></span></code></pre></div><p>3、查找不是北京和上海的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuaddress <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">in</span> (<span style="color:#e6db74">&#39;北京&#39;</span>,<span style="color:#e6db74">&#39;上海&#39;</span>);
</span></span></code></pre></div><h3 id="betweenandnot-betweenand">
  between…and|not between…and
  <a class="anchor" href="#betweenandnot-betweenand">#</a>
</h3>
<p>查找某个范围的记录</p>
<p>1、查找年龄在18~20之间的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuage<span style="color:#f92672">&gt;=</span><span style="color:#ae81ff">18</span> <span style="color:#66d9ef">and</span> stuage<span style="color:#f92672">&lt;=</span><span style="color:#ae81ff">20</span>;   <span style="color:#75715e"># 方法一
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuage <span style="color:#66d9ef">between</span> <span style="color:#ae81ff">18</span> <span style="color:#66d9ef">and</span> <span style="color:#ae81ff">20</span>;   <span style="color:#75715e"># 方法二
</span></span></span></code></pre></div><p>2、查找年龄不在18~20之间的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuage<span style="color:#f92672">&lt;</span><span style="color:#ae81ff">18</span> <span style="color:#66d9ef">or</span> stuage<span style="color:#f92672">&gt;</span><span style="color:#ae81ff">20</span>;		<span style="color:#75715e">#方法一
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> <span style="color:#66d9ef">not</span> (stuage<span style="color:#f92672">&gt;=</span><span style="color:#ae81ff">18</span> <span style="color:#66d9ef">and</span> stuage<span style="color:#f92672">&lt;=</span><span style="color:#ae81ff">20</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuage <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">between</span> <span style="color:#ae81ff">18</span> <span style="color:#66d9ef">and</span> <span style="color:#ae81ff">20</span>;
</span></span></code></pre></div><h3 id="is-null--is-not-null">
  is null | is not null
  <a class="anchor" href="#is-null--is-not-null">#</a>
</h3>
<p>脚下留心：查询一个为空的字段不能用等于，必须用is null</p>
<p>查找缺考的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> ch <span style="color:#66d9ef">is</span> <span style="color:#66d9ef">null</span> <span style="color:#66d9ef">or</span> math <span style="color:#66d9ef">is</span> <span style="color:#66d9ef">null</span>; <span style="color:#75715e"># 查找缺考的人
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>     <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>       <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">74</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span></code></pre></div><p>查找参加考试的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> ch <span style="color:#66d9ef">is</span> <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span> <span style="color:#66d9ef">and</span> math <span style="color:#66d9ef">is</span> <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">null</span>;
</span></span></code></pre></div><h3 id="聚合函数">
  聚合函数
  <a class="anchor" href="#%e8%81%9a%e5%90%88%e5%87%bd%e6%95%b0">#</a>
</h3>
<ol>
<li>
<p>sum()  	   求和</p>
</li>
<li>
<p>avg()          求平均值</p>
</li>
<li>
<p>max()        求最大值</p>
</li>
<li>
<p>min()          求最小值</p>
</li>
<li>
<p>count()      求记录数</p>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#75715e">#求语文总分、语文平均分、语文最高分、语文最低分、总人数
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">sum</span>(ch) <span style="color:#e6db74">&#39;语文总分&#39;</span>,<span style="color:#a6e22e">avg</span>(ch) <span style="color:#e6db74">&#39;语文平均分&#39;</span>, <span style="color:#a6e22e">max</span>(ch) <span style="color:#e6db74">&#39;语文最高分&#39;</span>,<span style="color:#a6e22e">min</span>(ch) <span style="color:#e6db74">&#39;语文最低分&#39;</span>,<span style="color:#a6e22e">count</span>(<span style="color:#f92672">*</span>) <span style="color:#e6db74">&#39;总人数&#39;</span> <span style="color:#66d9ef">from</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------------+------------+------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">语文总分</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">语文平均分</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">语文最高分</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">语文最低分</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">总人数</span>   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------------+------------+------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>      <span style="color:#ae81ff">597</span> <span style="color:#f92672">|</span>    <span style="color:#ae81ff">74</span>.<span style="color:#ae81ff">6250</span> <span style="color:#f92672">|</span>         <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>         <span style="color:#ae81ff">55</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">9</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------------+------------+------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="通配符">
  通配符
  <a class="anchor" href="#%e9%80%9a%e9%85%8d%e7%ac%a6">#</a>
</h3>
<ol>
<li>
<p>_  [下划线]   表示任意一个字符</p>
</li>
<li>
<p>%  表示任意字符</p>
</li>
</ol>
<p>练习</p>
<p>1、满足“T_m”的有（A、C）</p>
<p>A：Tom         B：Toom       C：Tam         D：Tm     E：Tmo</p>
<p>2、满足“T_m_”的有（B、C  ）</p>
<p>A:Tmom   B:Tmmm  C:T1m2    D:Tmm     E:Tm</p>
<p>3、满足“张%”的是（A、B、C、D）</p>
<p>A:张三     B：张三丰     C：张牙舞爪      D：张      E：小张</p>
<p>4、满足“%诺基亚%”的是（A、B、C、D）</p>
<p>A：诺基亚2100   B：2100诺基亚   C：把我的诺基亚拿过来   D：诺基亚</p>
<h3 id="模糊查询like">
  模糊查询（like）
  <a class="anchor" href="#%e6%a8%a1%e7%b3%8a%e6%9f%a5%e8%af%a2like">#</a>
</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#75715e"># 查找姓张的同学
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuname <span style="color:#66d9ef">like</span> <span style="color:#e6db74">&#39;张%&#39;</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>     <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span><span style="color:#75715e">#例题
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuname <span style="color:#66d9ef">like</span> <span style="color:#e6db74">&#39;T_m&#39;</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25320 <span style="color:#f92672">|</span> Tom     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">24</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">8</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">67</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="order-by排序">
  order by排序
  <a class="anchor" href="#order-by%e6%8e%92%e5%ba%8f">#</a>
</h3>
<p>asc：升序【默认】</p>
<p>desc：降序</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> ch <span style="color:#66d9ef">desc</span>;		<span style="color:#75715e"># 语文成绩降序排列
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> math <span style="color:#66d9ef">asc</span>;     <span style="color:#75715e"># 数学成绩升序排列
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> math;       <span style="color:#75715e"># 默认升序排列
</span></span></span></code></pre></div><p>多列排序</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#75715e">#年龄升序,成绩降序
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span>,(ch<span style="color:#f92672">+</span>math) <span style="color:#66d9ef">as</span> <span style="color:#e6db74">&#39;总分&#39;</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> stuage <span style="color:#66d9ef">asc</span>,(ch<span style="color:#f92672">+</span>math) <span style="color:#66d9ef">desc</span>;
</span></span></code></pre></div><p>思考如下代码表示什么含义</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> stuage <span style="color:#66d9ef">desc</span>,ch <span style="color:#66d9ef">desc</span>;     <span style="color:#75715e">#年龄降序，语文降序
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> stuage <span style="color:#66d9ef">desc</span>,ch <span style="color:#66d9ef">asc</span>;		<span style="color:#75715e">#年龄降序，语文升序
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> stuage,ch <span style="color:#66d9ef">desc</span>;          <span style="color:#75715e">#年龄升序、语文降序
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> stuage,ch; 				<span style="color:#75715e">#年龄升序、语文升序
</span></span></span></code></pre></div><h3 id="group-by-分组查询">
  group by 【分组查询】
  <a class="anchor" href="#group-by-%e5%88%86%e7%bb%84%e6%9f%a5%e8%af%a2">#</a>
</h3>
<p>将查询的结果分组，分组查询目的在于统计数据。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#75715e"># 按性别分组，显示每组的平均年龄
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">avg</span>(stuage) <span style="color:#66d9ef">as</span> <span style="color:#e6db74">&#39;年龄&#39;</span>,stusex <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">group</span> <span style="color:#66d9ef">by</span> stusex;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">年龄</span>     <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">22</span>.<span style="color:#ae81ff">7500</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">25</span>.<span style="color:#ae81ff">4000</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 按地区分组，每个地区的平均年龄
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">avg</span>(stuage) <span style="color:#66d9ef">as</span> <span style="color:#e6db74">&#39;年龄&#39;</span>,stuaddress <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">group</span> <span style="color:#66d9ef">by</span> stuaddress;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">年龄</span>     <span style="color:#f92672">|</span> stuaddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">31</span>.<span style="color:#ae81ff">0000</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">21</span>.<span style="color:#ae81ff">3333</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">27</span>.<span style="color:#ae81ff">0000</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">23</span>.<span style="color:#ae81ff">0000</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">23</span>.<span style="color:#ae81ff">0000</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河南</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">5</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><pre tabindex="0"><code>脚下留心：1、如果是分组查询，查询字段必须是分组字段和聚合函数。2、查询字段是普通字段，只取第一个值
</code></pre><p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032305858.png" alt="" /></p>
<p>通过group_concat()函数将同一组的值连接起来显示</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">group_concat</span>(stuname),stusex <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">group</span> <span style="color:#66d9ef">by</span> stusex;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------------------------------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">group_concat</span>(stuname)               <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------------------------------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>,<span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>,<span style="color:#960050;background-color:#1e0010">梅超风</span>,Tabm            <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>,<span style="color:#960050;background-color:#1e0010">李文才</span>,<span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>,<span style="color:#960050;background-color:#1e0010">争青小子</span>,Tom     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------------------------------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><pre tabindex="0"><code>多学一招：【了解】1、分组后的结果默认会按升序排列显示2、也是可以使用desc实现分组后的降序
</code></pre><p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032306116.png" alt="" /></p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032306725.png" alt="" /></p>
<p>多列分组</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuaddress,stusex,<span style="color:#a6e22e">avg</span>(stuage) <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">group</span> <span style="color:#66d9ef">by</span> stuaddress,stusex;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+--------+-------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuaddress <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span> <span style="color:#a6e22e">avg</span>(stuage) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+--------+-------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span>.<span style="color:#ae81ff">0000</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span>.<span style="color:#ae81ff">0000</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">21</span>.<span style="color:#ae81ff">0000</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">27</span>.<span style="color:#ae81ff">0000</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span>.<span style="color:#ae81ff">0000</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河南</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span>.<span style="color:#ae81ff">0000</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+--------+-------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">6</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h3 id="having条件">
  having条件
  <a class="anchor" href="#having%e6%9d%a1%e4%bb%b6">#</a>
</h3>
<pre tabindex="0"><code>思考：数据库中的表是一个二维表，返回的结果是一张二维表，既然能在数据库的二维表中进行查询，能否在结果集的二维表上继续进行查询？答：可以，having条件就是在结果集上继续进行筛选。
</code></pre><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stusex<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;男&#39;</span>;   <span style="color:#75715e"># 从数据库中查找
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">76</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>       <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">74</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">86</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">92</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25320 <span style="color:#f92672">|</span> Tom      <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">24</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">8</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">67</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">5</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">having</span> stusex<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;男&#39;</span>;   <span style="color:#75715e"># 从结果集中查找
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>      <span style="color:#f92672">|</span>   <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>      <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">76</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>      <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">74</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>      <span style="color:#f92672">|</span>   <span style="color:#ae81ff">86</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">92</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25320 <span style="color:#f92672">|</span> Tom      <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">24</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">8</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>      <span style="color:#f92672">|</span>   <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">67</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">5</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>思考如下语句是否正确 <img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032306971.png" alt="" /></p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032306851.png" alt="" /></p>
<p>having和where的区别：</p>
<p>where是对原始数据进行筛选，having是对记录集进行筛选。</p>
<h3 id="limit">
  limit
  <a class="anchor" href="#limit">#</a>
</h3>
<p>语法：limit 起始位置，显示长度</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">limit</span> <span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">2</span>;    <span style="color:#75715e"># 从0的位置开始，取两条数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">76</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span> 
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">limit</span> <span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">2</span>;    <span style="color:#75715e"># 从2的位置开始，取两条数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>     <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">55</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>        <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">74</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span></code></pre></div><p>起始位置可以省略，默认是从0开始</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">limit</span> <span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">76</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>例题：找出班级总分前三名</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span>,(ch<span style="color:#f92672">+</span>math) total <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> total <span style="color:#66d9ef">desc</span> <span style="color:#66d9ef">limit</span> <span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">3</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span> total <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>     <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">86</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">92</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">178</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25321 <span style="color:#f92672">|</span> Tabm     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">9</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">165</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>     <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">76</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">153</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+-------+</span>
</span></span></code></pre></div><p>多学一招：limit在update和delete语句中也是可以使用的。</p>
<h3 id="查询语句中的选项">
  查询语句中的选项
  <a class="anchor" href="#%e6%9f%a5%e8%af%a2%e8%af%ad%e5%8f%a5%e4%b8%ad%e7%9a%84%e9%80%89%e9%a1%b9">#</a>
</h3>
<p>查询语句中的选项有两个：</p>
<p>1、     all：显示所有数据 【默认】</p>
<p>2、     distinct：去除结果集中重复的数据</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#66d9ef">distinct</span> stuaddress <span style="color:#66d9ef">from</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuaddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河南</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">5</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h2 id="union联合">
  union（联合）
  <a class="anchor" href="#union%e8%81%94%e5%90%88">#</a>
</h2>
<p>插入测试数据</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">GO1</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span> <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>));
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> Go1 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1</span>,<span style="color:#e6db74">&#39;李白&#39;</span>),(<span style="color:#ae81ff">2</span>,<span style="color:#e6db74">&#39;张秋丽&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">2</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">2</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h4 id="union的使用">
  union的使用
  <a class="anchor" href="#union%e7%9a%84%e4%bd%bf%e7%94%a8">#</a>
</h4>
<p>作用：将多个select语句结果集纵向联合起来</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">select</span> <span style="color:#960050;background-color:#1e0010">语句</span> <span style="color:#66d9ef">union</span> [<span style="color:#960050;background-color:#1e0010">选项</span>] <span style="color:#66d9ef">select</span> <span style="color:#960050;background-color:#1e0010">语句</span> <span style="color:#66d9ef">union</span> [<span style="color:#960050;background-color:#1e0010">选项</span>] <span style="color:#66d9ef">select</span> <span style="color:#960050;background-color:#1e0010">语句</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuno,stuname <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">union</span> <span style="color:#66d9ef">select</span> id,name <span style="color:#66d9ef">from</span> Go1;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuno  <span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25305 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25319 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25320 <span style="color:#f92672">|</span> Tom      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25321 <span style="color:#f92672">|</span> Tabm     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">1</span>      <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李白</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">2</span>      <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+</span>
</span></span></code></pre></div><p>例题：查询上海的男生和北京的女生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuname,stuaddress,stusex <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> (stuaddress<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;上海&#39;</span> <span style="color:#66d9ef">and</span> stusex<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;男&#39;</span>) <span style="color:#66d9ef">or</span> (stuaddress<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;北京&#39;</span> <span style="color:#66d9ef">and</span> stusex<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;女&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname <span style="color:#f92672">|</span> stuaddress <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuname,stuaddress,stusex <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuaddress<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;上海&#39;</span> <span style="color:#66d9ef">and</span> stusex<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;男&#39;</span> <span style="color:#66d9ef">union</span> <span style="color:#66d9ef">select</span> stuname,stuaddress,stusex <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> stuaddress<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;北京&#39;</span> <span style="color:#66d9ef">and</span> stusex<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;女&#39;</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname <span style="color:#f92672">|</span> stuaddress <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+------------+--------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span></code></pre></div><h4 id="union的选项">
  union的选项
  <a class="anchor" href="#union%e7%9a%84%e9%80%89%e9%a1%b9">#</a>
</h4>
<p>union的选项有两个</p>
<p>1、     all：显示所有数据</p>
<p>2、     distinct：去除重复的数据【默认】</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> name <span style="color:#66d9ef">from</span> go1 <span style="color:#66d9ef">union</span> <span style="color:#66d9ef">select</span> stuname <span style="color:#66d9ef">from</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李白</span>     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Tom      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Tabm     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+</span>
</span></span></code></pre></div><p>默认是去重复的</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> name <span style="color:#66d9ef">from</span> go1 <span style="color:#66d9ef">union</span> <span style="color:#66d9ef">all</span> <span style="color:#66d9ef">select</span> stuname <span style="color:#66d9ef">from</span> stu;  <span style="color:#75715e"># all不去重复记录
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> name     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李白</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Tom      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Tabm     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+</span>
</span></span></code></pre></div><h4 id="union的注意事项">
  union的注意事项
  <a class="anchor" href="#union%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9">#</a>
</h4>
<p>1、     union两边的select语句的字段个数必须一致</p>
<p>2、     union两边的select语句的字段名可以不一致，最终按第一个select语句的字段名。</p>
<p>3、     union两边的select语句中的数据类型可以不一致。</p>
<h2 id="多表查询分类">
  多表查询分类
  <a class="anchor" href="#%e5%a4%9a%e8%a1%a8%e6%9f%a5%e8%af%a2%e5%88%86%e7%b1%bb">#</a>
</h2>
<p>将多个表的数据横向的联合起来。
1、	内连接
2、	外连接
a)	左外连接
b)	右外连接
3、	交叉连接
4、	自然连接</p>
<h4 id="内连接inner-join">
  内连接【inner join】
  <a class="anchor" href="#%e5%86%85%e8%bf%9e%e6%8e%a5inner-join">#</a>
</h4>
<pre tabindex="0"><code>语法一：select 列名 from 表1 inner join 表2 on 表1.公共字段=表2.公共字段

语法二：select 列名 from 表1,表2 where 表1.公共字段=表2.公共字段
</code></pre><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">方法一：</span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuname,stusex,writtenexam,labexam <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">inner</span> <span style="color:#66d9ef">join</span> stumarks <span style="color:#66d9ef">on</span> stuinfo.stuno<span style="color:#f92672">=</span>stumarks.stuno;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span> writtenexam <span style="color:#f92672">|</span> labexam <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>          <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">58</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">90</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">56</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">48</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">方法二：</span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuinfo.stuno,stuname,stusex,writtenexam,labexam <span style="color:#66d9ef">from</span> stuinfo,stumarks <span style="color:#66d9ef">where</span> stuinfo.stuno<span style="color:#f92672">=</span>stumarks.stuno;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuno  <span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span> writtenexam <span style="color:#f92672">|</span> labexam <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>          <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">58</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">90</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">56</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">48</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">可以给表取别名</span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> i.stuno,stuname,stusex,writtenexam,labexam <span style="color:#66d9ef">from</span> stuinfo i,stumarks s <span style="color:#66d9ef">where</span> i.stuno<span style="color:#f92672">=</span>s.stuno;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuno  <span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span> writtenexam <span style="color:#f92672">|</span> labexam <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>          <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">58</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">90</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">56</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">48</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">5</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>脚下留心：显示公共字段需要指定表名 <img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032307721.png" alt="" /></p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032307265.png" alt="" /></p>
<pre tabindex="0"><code>思考：
select * from 表1 inner join 表2 on 表1.公共字段=表2.公共字段  和
select * from 表2 inner join 表1 on 表1.公共字段=表2.公共字段   结果是否一样？
答：一样的，因为内连接获取的是两个表的公共部分
</code></pre><pre tabindex="0"><code>多学一招：三个表的内连接如何实现？
select * from 表1 inner join 表2 on 表1.公共字段=表2.公共字段
inner join 表3 on 表2.公共字段=表3.公共字段
</code></pre><h4 id="左外连接left-join">
  左外连接【left join】
  <a class="anchor" href="#%e5%b7%a6%e5%a4%96%e8%bf%9e%e6%8e%a5left-join">#</a>
</h4>
<p>以左边的表为标准，如果右边的表没有对应的记录，用NULL填充。</p>
<pre tabindex="0"><code>语法：select 列名 from 表1 left join 表2 on 表1.公共字段=表2.公共字段
</code></pre><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuname,writtenexam,labexam <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">left</span> <span style="color:#66d9ef">join</span> stumarks <span style="color:#66d9ef">on</span> stuinfo.stuno<span style="color:#f92672">=</span>stumarks.stuno;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span> writtenexam <span style="color:#f92672">|</span> labexam <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span>          <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span>          <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">90</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span>          <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">58</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span>          <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>         <span style="color:#f92672">|</span>        <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>    <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span>          <span style="color:#ae81ff">56</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">48</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>        <span style="color:#f92672">|</span>        <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>    <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+-------------+---------+</span>
</span></span></code></pre></div><pre tabindex="0"><code>思考：
select * from 表1 left join 表2 on 表1.公共字段=表2.公共字段
和
select * from 表2 left join 表1 on 表1.公共字段=表2.公共字段   是否一样？
答：不一样，左连接一左边的表为准。
</code></pre><h4 id="右外连接right-join">
  右外连接【right join】
  <a class="anchor" href="#%e5%8f%b3%e5%a4%96%e8%bf%9e%e6%8e%a5right-join">#</a>
</h4>
<p>以右边的表为标准，如果左边的表没有对应的记录，用NULL填充。</p>
<pre tabindex="0"><code>语法：select 列名 from 表1 right join 表2 on 表1.公共字段=表2.公共字段
</code></pre><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuname,writtenexam,labexam <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">right</span> <span style="color:#66d9ef">join</span> stumarks <span style="color:#66d9ef">on</span> stuinfo.stuno<span style="color:#f92672">=</span>stumarks.stuno;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span> writtenexam <span style="color:#f92672">|</span> labexam <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span>          <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">58</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span>          <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">90</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span>          <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span>          <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span>          <span style="color:#ae81ff">56</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">48</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span>          <span style="color:#ae81ff">66</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">6</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><pre tabindex="0"><code>思考：select * from 表1 left join 表2 on 表1.公共字段=表2.公共字段和select * from 表2 right join 表1 on 表1.公共字段=表2.公共字段  是否一样？答：一样的
</code></pre><h4 id="交叉连接cross-join">
  交叉连接【cross join】
  <a class="anchor" href="#%e4%ba%a4%e5%8f%89%e8%bf%9e%e6%8e%a5cross-join">#</a>
</h4>
<p>插入测试数据</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t1</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t1 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1</span>,<span style="color:#e6db74">&#39;tom&#39;</span>),(<span style="color:#ae81ff">2</span>,<span style="color:#e6db74">&#39;berry&#39;</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">2</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">t2</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> score <span style="color:#66d9ef">int</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> t2 <span style="color:#66d9ef">values</span> (<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">88</span>),(<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">99</span>);
</span></span></code></pre></div><p>1、如果没有连接表达式返回的是笛卡尔积</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t1 <span style="color:#66d9ef">cross</span> <span style="color:#66d9ef">join</span> t2;   <span style="color:#75715e"># 返回笛卡尔积
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+------+-------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id   <span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> id   <span style="color:#f92672">|</span> score <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+-------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> tom   <span style="color:#f92672">|</span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span>    <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> berry <span style="color:#f92672">|</span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span>    <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> tom   <span style="color:#f92672">|</span>    <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span>    <span style="color:#ae81ff">99</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> berry <span style="color:#f92672">|</span>    <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span>    <span style="color:#ae81ff">99</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+-------+------+-------+</span>
</span></span></code></pre></div><p>2、如果有连接表达式等价于内连接</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> t1 <span style="color:#66d9ef">cross</span> <span style="color:#66d9ef">join</span> t2 <span style="color:#66d9ef">where</span> t1.id<span style="color:#f92672">=</span>t2.id;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+-------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> id   <span style="color:#f92672">|</span> name  <span style="color:#f92672">|</span> id   <span style="color:#f92672">|</span> score <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+-------+------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> tom   <span style="color:#f92672">|</span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span>    <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>    <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> berry <span style="color:#f92672">|</span>    <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span>    <span style="color:#ae81ff">99</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+-------+------+-------+</span>
</span></span></code></pre></div><h4 id="自然连接natural">
  自然连接【natural】
  <a class="anchor" href="#%e8%87%aa%e7%84%b6%e8%bf%9e%e6%8e%a5natural">#</a>
</h4>
<pre tabindex="0"><code>自动的判断连接条件，它是过同名字段来判断的
</code></pre><p>自然连接又分为：</p>
<ol>
<li>自然内连接		natural join</li>
<li>自然左外连接		natural left join</li>
<li>自然右外连接		natural right join</li>
</ol>
<p>例题：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#75715e"># 自然内连接
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">natural</span> <span style="color:#66d9ef">join</span> stumarks;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+---------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> examNo  <span style="color:#f92672">|</span> writtenExam <span style="color:#f92672">|</span> labExam <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+---------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span> s271811 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>  <span style="color:#ae81ff">58</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>          <span style="color:#f92672">|</span> s271813 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>  <span style="color:#ae81ff">90</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>           <span style="color:#f92672">|</span> s271815 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span> s271816 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>           <span style="color:#f92672">|</span> s271819 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">56</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">48</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+---------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">5</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 自然左外连接
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">natural</span> <span style="color:#66d9ef">left</span> <span style="color:#66d9ef">join</span> stumarks;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+---------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> examNo  <span style="color:#f92672">|</span> writtenExam <span style="color:#f92672">|</span> labExam <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+---------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span> s271816 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">77</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>          <span style="color:#f92672">|</span> s271813 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>  <span style="color:#ae81ff">90</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span> s271811 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>  <span style="color:#ae81ff">58</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>           <span style="color:#f92672">|</span> s271815 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">65</span>
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25305 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">7</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河南</span>           <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>        <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>   <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>           <span style="color:#f92672">|</span> s271819 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">56</span>
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">48</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25319 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">5</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>          <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>        <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>ULL <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+---------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">7</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 自然右外连接
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">natural</span> <span style="color:#66d9ef">right</span> <span style="color:#66d9ef">join</span> stumarks;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+-------------+---------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> examNo  <span style="color:#f92672">|</span> writtenExam <span style="color:#f92672">|</span> labExam <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+-------------+---------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> s271811 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">58</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>
</span></span><span style="display:flex;"><span>     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> s271813 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">90</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>
</span></span><span style="display:flex;"><span>     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> s271815 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> s271816 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>
</span></span><span style="display:flex;"><span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> s271819 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">56</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">48</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25320 <span style="color:#f92672">|</span> s271820 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">66</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>   <span style="color:#f92672">|</span>   <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>    <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+-------------+---------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">6</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>自然连接结论：</p>
<ol>
<li>
<p>表连接通过同名的字段来连接的</p>
</li>
<li>
<p>如果没有同名的字段返回笛卡尔积</p>
</li>
<li>
<p>会对结果进行整理，整理的规则如下</p>
<p>a)	连接字段保留一个</p>
<p>b)	连接字段放在最前面</p>
<p>c)     左外连接左边在前，右外连接右表在前</p>
</li>
</ol>
<h4 id="using">
  using()
  <a class="anchor" href="#using">#</a>
</h4>
<ol>
<li>
<p>用来指定连接字段。</p>
</li>
<li>
<p>using()也会对连接字段进行整理，整理方式和自然连接是一样的。</p>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">inner</span> <span style="color:#66d9ef">join</span> stumarks <span style="color:#66d9ef">using</span>(stuno);   <span style="color:#75715e"># using指定字段
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+---------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> examNo  <span style="color:#f92672">|</span> writtenExam <span style="color:#f92672">|</span> labExam <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+---------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span> s271811 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>  <span style="color:#ae81ff">58</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>          <span style="color:#f92672">|</span> s271813 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>  <span style="color:#ae81ff">90</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>           <span style="color:#f92672">|</span> s271815 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span> s271816 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>           <span style="color:#f92672">|</span> s271819 <span style="color:#f92672">|</span>          <span style="color:#ae81ff">56</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">48</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+---------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">5</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h2 id="子查询">
  子查询
  <a class="anchor" href="#%e5%ad%90%e6%9f%a5%e8%af%a2">#</a>
</h2>
<p>语法</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法：</span><span style="color:#66d9ef">select</span> <span style="color:#960050;background-color:#1e0010">语句</span> <span style="color:#66d9ef">where</span> <span style="color:#960050;background-color:#1e0010">条件</span> (<span style="color:#66d9ef">select</span> <span style="color:#960050;background-color:#1e0010">…</span> <span style="color:#66d9ef">from</span> <span style="color:#960050;background-color:#1e0010">表</span>)
</span></span></code></pre></div><ol>
<li>外面的查询称为父查询，括号中的查询称为子查询</li>
<li>子查询为父查询提供查询条件</li>
</ol>
<h4 id="例题-1">
  例题
  <a class="anchor" href="#%e4%be%8b%e9%a2%98-1">#</a>
</h4>
<p>1、查找笔试80分的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> stuno<span style="color:#f92672">=</span>(<span style="color:#66d9ef">select</span> stuno <span style="color:#66d9ef">from</span> stumarks <span style="color:#66d9ef">where</span> writtenexam<span style="color:#f92672">=</span><span style="color:#ae81ff">80</span>);
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>       <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span></code></pre></div><p>2、查找笔试最高分的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#75715e"># 方法一：
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> stuno<span style="color:#f92672">=</span>(<span style="color:#66d9ef">select</span> stuno <span style="color:#66d9ef">from</span> stumarks <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> writtenexam <span style="color:#66d9ef">desc</span> <span style="color:#66d9ef">limit</span>
</span></span><span style="display:flex;"><span> <span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>       <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 方法二：
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> stuno<span style="color:#f92672">=</span>(<span style="color:#66d9ef">select</span> stuno <span style="color:#66d9ef">from</span> stumarks <span style="color:#66d9ef">where</span> writtenexam<span style="color:#f92672">=</span>(<span style="color:#66d9ef">select</span> <span style="color:#a6e22e">max</span>(writtenexam) <span style="color:#66d9ef">from</span> stumarks));
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>       <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><pre tabindex="0"><code>脚下留心：上面的例题，子查询只能返回一个值。如果子查询返回多个值就不能用“=”了,需要用 in
</code></pre><h4 id="innot-in子查询">
  in|not in子查询
  <a class="anchor" href="#innot-in%e5%ad%90%e6%9f%a5%e8%af%a2">#</a>
</h4>
<p>用于子查询的返回结果多个值。</p>
<p>1、查找笔试成绩及格的同学</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> stuno <span style="color:#66d9ef">in</span> (<span style="color:#66d9ef">select</span> stuno <span style="color:#66d9ef">from</span> stumarks <span style="color:#66d9ef">where</span> writtenexam<span style="color:#f92672">&gt;=</span><span style="color:#ae81ff">60</span>);
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">3</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>2、查询不及格的同学</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> stuno  <span style="color:#66d9ef">in</span> (<span style="color:#66d9ef">select</span> stuno <span style="color:#66d9ef">from</span> stumarks <span style="color:#66d9ef">where</span> writtenexam<span style="color:#f92672">&lt;=</span><span style="color:#ae81ff">60</span>);
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span></code></pre></div><p>3、查询没有通过的同学（不及格，缺考）</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> stuno  <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">in</span> (<span style="color:#66d9ef">select</span> stuno <span style="color:#66d9ef">from</span> stumarks <span style="color:#66d9ef">where</span> writtenexam<span style="color:#f92672">&gt;=</span><span style="color:#ae81ff">60</span>);
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25305 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">7</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河南</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25319 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">5</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span><span style="color:#ae81ff">4</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="exists和not-exists">
  exists和not exists
  <a class="anchor" href="#exists%e5%92%8cnot-exists">#</a>
</h4>
<p>1、	如果有人笔试超过80分就显示所有的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> <span style="color:#66d9ef">exists</span> (<span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stumarks <span style="color:#66d9ef">where</span> writtenexam<span style="color:#f92672">&gt;=</span><span style="color:#ae81ff">80</span>);
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25305 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">7</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河南</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25319 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">5</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span></code></pre></div><p>2、	如果没有人超过80分就显示所有的学生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> <span style="color:#66d9ef">not</span> <span style="color:#66d9ef">exists</span> (<span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stumarks <span style="color:#66d9ef">where</span> writtenexam<span style="color:#f92672">&gt;=</span><span style="color:#ae81ff">80</span>);Empty <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span></code></pre></div><h4 id="子查询分类">
  子查询分类
  <a class="anchor" href="#%e5%ad%90%e6%9f%a5%e8%af%a2%e5%88%86%e7%b1%bb">#</a>
</h4>
<p>1、标量子查询：子查询返回的结果就一个</p>
<p>2、列子查询：子查询返回的结果是一个列表</p>
<p>3、行子查询：子查询返回的结果是一行</p>
<p>例题：查询成绩最高的男生和女生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuname,stusex,ch <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">where</span> (stusex,ch) <span style="color:#66d9ef">in</span> (<span style="color:#66d9ef">select</span> stusex,<span style="color:#a6e22e">max</span>(ch) <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">group</span> <span style="color:#66d9ef">by</span> stusex);
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">86</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Tabm     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>   <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+------+</span>
</span></span></code></pre></div><p>4、表子查询：子查询返回的结果当成一个表</p>
<p>例题：查询成绩最高的男生和女生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuname,stusex,ch <span style="color:#66d9ef">from</span> (<span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> ch <span style="color:#66d9ef">desc</span>) <span style="color:#66d9ef">as</span> t <span style="color:#66d9ef">group</span> <span style="color:#66d9ef">by</span> stusex;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Tabm     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>   <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">86</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+------+</span>
</span></span></code></pre></div><pre tabindex="0"><code>脚下留心：from后面是一个表，如果子查询的结果当成表来看，必须将子查询的结果取别名。
</code></pre><h2 id="视图view">
  视图【view】
  <a class="anchor" href="#%e8%a7%86%e5%9b%beview">#</a>
</h2>
<p>1、	视图是一张虚拟表，它表示一张表的部分或多张表的综合的结构。</p>
<p>2、	视图仅仅是表结构，没有表数据。视图的结构和数据建立在表的基础上。</p>
<h4 id="创建视图">
  创建视图
  <a class="anchor" href="#%e5%88%9b%e5%bb%ba%e8%a7%86%e5%9b%be">#</a>
</h4>
<p>语法</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">create</span> [<span style="color:#66d9ef">or</span> <span style="color:#66d9ef">replace</span>] view <span style="color:#960050;background-color:#1e0010">视图的名称</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">as</span>
</span></span><span style="display:flex;"><span>	select语句
</span></span></code></pre></div><p>例题：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> view vw_stu
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">as</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">select</span> stuname,stusex,writtenexam,labexam <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">inner</span> <span style="color:#66d9ef">join</span> stumarks <span style="color:#66d9ef">using</span>(stuno);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><pre tabindex="0"><code>多学一招：因为视图是一个表结构，所以创建视图后，会在数据库文件夹中多一个与视图名同名的.frm文件
</code></pre><h4 id="使用视图">
  使用视图
  <a class="anchor" href="#%e4%bd%bf%e7%94%a8%e8%a7%86%e5%9b%be">#</a>
</h4>
<p>视图是一张虚拟表，视图的用法和表的用法一样</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> vw_stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span> stusex <span style="color:#f92672">|</span> writtenexam <span style="color:#f92672">|</span> labexam <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>          <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">58</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>          <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">90</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>         <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">50</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>         <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>          <span style="color:#ae81ff">56</span> <span style="color:#f92672">|</span>      <span style="color:#ae81ff">48</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+--------+-------------+---------+</span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">update</span> vw_stu <span style="color:#66d9ef">set</span> writtenexam<span style="color:#f92672">=</span><span style="color:#ae81ff">88</span> <span style="color:#66d9ef">where</span> stuname<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;李斯文&#39;</span>;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">05</span> sec)Rows matched: <span style="color:#ae81ff">1</span>  Changed: <span style="color:#ae81ff">1</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h4 id="查看视图的结构">
  查看视图的结构
  <a class="anchor" href="#%e6%9f%a5%e7%9c%8b%e8%a7%86%e5%9b%be%e7%9a%84%e7%bb%93%e6%9e%84">#</a>
</h4>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">desc</span> <span style="color:#960050;background-color:#1e0010">视图名</span>
</span></span></code></pre></div><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">desc</span> vw_stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------------+-------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Field       <span style="color:#f92672">|</span> Type        <span style="color:#f92672">|</span> <span style="color:#66d9ef">Null</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Key</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">Default</span> <span style="color:#f92672">|</span> Extra <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------------+-------------+------+-----+---------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname     <span style="color:#f92672">|</span> <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>) <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stusex      <span style="color:#f92672">|</span> <span style="color:#66d9ef">char</span>(<span style="color:#ae81ff">2</span>)     <span style="color:#f92672">|</span> NO   <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> writtenexam <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)     <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> labexam     <span style="color:#f92672">|</span> <span style="color:#66d9ef">int</span>(<span style="color:#ae81ff">11</span>)     <span style="color:#f92672">|</span> YES  <span style="color:#f92672">|</span>     <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span>    <span style="color:#f92672">|</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-------------+-------------+------+-----+---------+-------+</span>
</span></span></code></pre></div><h4 id="查看创建视图的语法">
  查看创建视图的语法
  <a class="anchor" href="#%e6%9f%a5%e7%9c%8b%e5%88%9b%e5%bb%ba%e8%a7%86%e5%9b%be%e7%9a%84%e8%af%ad%e6%b3%95">#</a>
</h4>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> view <span style="color:#960050;background-color:#1e0010">视图名</span>
</span></span></code></pre></div><p>例题</p>
<p><img src="https://blockchaincode.oss-cn-hangzhou.aliyuncs.com/Hexo_img/202208032308596.png" alt="" /></p>
<h4 id="显示所有视图">
  显示所有视图
  <a class="anchor" href="#%e6%98%be%e7%a4%ba%e6%89%80%e6%9c%89%e8%a7%86%e5%9b%be">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span> <span style="color:#75715e">#方法一：mysql&gt; show tables;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> <span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">|</span> Tables_in_itcast <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">|</span> stu              <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">|</span> stuinfo          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">|</span> stumarks         <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">|</span> t1               <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">|</span> t2               <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">|</span> vw_stu           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 方法二
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> table_name <span style="color:#66d9ef">from</span> information_schema.views;
</span></span><span style="display:flex;"><span> <span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">|</span> table_name <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">|</span> vw_stu     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span> <span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">05</span> sec)
</span></span><span style="display:flex;"><span> <span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span> <span style="color:#75715e">#方法三
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">table</span> status <span style="color:#66d9ef">where</span> comment<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;view&#39;</span> <span style="color:#960050;background-color:#1e0010">\</span>G
</span></span><span style="display:flex;"><span><span style="color:#f92672">***************************</span> <span style="color:#ae81ff">1</span>. row <span style="color:#f92672">***************************</span>
</span></span><span style="display:flex;"><span>           Name: vw_stu
</span></span><span style="display:flex;"><span>         <span style="color:#66d9ef">Engine</span>: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>        Version: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>     Row_format: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>           Rows: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span> Avg_row_length: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>    Data_length: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>Max_data_length: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>   Index_length: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>      Data_free: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">Auto_increment</span>: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>    Create_time: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>    Update_time: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>     Check_time: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>      Collation: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>       Checksum: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span> Create_options: <span style="color:#66d9ef">NULL</span>
</span></span><span style="display:flex;"><span>        Comment: VIEW
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="更改视图">
  更改视图
  <a class="anchor" href="#%e6%9b%b4%e6%94%b9%e8%a7%86%e5%9b%be">#</a>
</h4>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">alter</span> view <span style="color:#960050;background-color:#1e0010">视图名</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">as</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">select</span> <span style="color:#960050;background-color:#1e0010">语句</span>
</span></span></code></pre></div><p>例题：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> view vw_stu
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">as</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="删除视图">
  删除视图
  <a class="anchor" href="#%e5%88%a0%e9%99%a4%e8%a7%86%e5%9b%be">#</a>
</h4>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">drop</span> view [<span style="color:#66d9ef">if</span> <span style="color:#66d9ef">exists</span>] <span style="color:#960050;background-color:#1e0010">视图</span><span style="color:#ae81ff">1</span>,<span style="color:#960050;background-color:#1e0010">视图</span><span style="color:#ae81ff">2</span>,<span style="color:#960050;background-color:#1e0010">…</span>
</span></span></code></pre></div><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">drop</span> view vw_stu;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="视图的作用">
  视图的作用
  <a class="anchor" href="#%e8%a7%86%e5%9b%be%e7%9a%84%e4%bd%9c%e7%94%a8">#</a>
</h4>
<ol>
<li>筛选数据，防止未经许可访问敏感数据</li>
<li>隐藏表结构</li>
<li>降低SQL语句的复杂度</li>
</ol>
<h4 id="视图的算法">
  视图的算法
  <a class="anchor" href="#%e8%a7%86%e5%9b%be%e7%9a%84%e7%ae%97%e6%b3%95">#</a>
</h4>
<p>场景：找出语文成绩最高的男生和女生</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> (<span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> ch <span style="color:#66d9ef">desc</span>) <span style="color:#66d9ef">as</span> t <span style="color:#66d9ef">group</span> <span style="color:#66d9ef">by</span> stusex;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25321 <span style="color:#f92672">|</span> Tabm     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">9</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>      <span style="color:#f92672">|</span>   <span style="color:#ae81ff">86</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">92</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span></code></pre></div><p>我们可以将子查询封装到视图中</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> view vw_stu
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">as</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> ch <span style="color:#66d9ef">desc</span>;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>可以将上面的子查询更改成视图，但是，结果和上面不一样</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> vw_stu <span style="color:#66d9ef">group</span> <span style="color:#66d9ef">by</span> stusex;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>     <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>     <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">55</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+------+------+</span>
</span></span></code></pre></div><p>原因：这是因为视图的算法造成的</p>
<pre tabindex="0"><code>1. merge：合并算法，将视图的语句和外层的语句合并后在执行。
2. temptable：临时表算法，将视图生成一个临时表，再执行外层语句
3. undefined：未定义，MySQL到底用merge还是用temptable由MySQL决定，这是一个默认的算法，一般视图都会选择merge算法，因为merge效率高。
</code></pre><p>解决：在创建视图的时候指定视图的算法</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">create</span> algorithm<span style="color:#f92672">=</span>temptable view <span style="color:#960050;background-color:#1e0010">视图名</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">as</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">select</span> <span style="color:#960050;background-color:#1e0010">语句</span>
</span></span></code></pre></div><p>指定算法创建视图</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> algorithm<span style="color:#f92672">=</span>temptable view vw_stu    
</span></span><span style="display:flex;"><span><span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">as</span>    
</span></span><span style="display:flex;"><span><span style="color:#f92672">-&gt;</span>  <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stu <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> ch <span style="color:#66d9ef">desc</span>;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> vw_stu <span style="color:#66d9ef">group</span> <span style="color:#66d9ef">by</span> stusex;  
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 结果是一致的
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25321 <span style="color:#f92672">|</span> Tabm     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>     <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">9</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>       <span style="color:#f92672">|</span>   <span style="color:#ae81ff">86</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">92</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+------+------+</span>
</span></span></code></pre></div><h2 id="事务transaction">
  事务transaction
  <a class="anchor" href="#%e4%ba%8b%e5%8a%a1transaction">#</a>
</h2>
<ol>
<li>事务是一个不可分割的执行单元</li>
<li>事务作为一个整体要么一起执行，要么一起回滚</li>
</ol>
<p>插入测试数据</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">bank</span>(
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">-&gt;</span> cardid <span style="color:#66d9ef">char</span>(<span style="color:#ae81ff">4</span>) <span style="color:#66d9ef">primary</span> <span style="color:#66d9ef">key</span>,
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">-&gt;</span> money <span style="color:#66d9ef">int</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> bank <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;1001&#39;</span>,<span style="color:#ae81ff">1000</span>),(<span style="color:#e6db74">&#39;1002&#39;</span>,<span style="color:#ae81ff">100</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">2</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">2</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h4 id="事务操作">
  事务操作
  <a class="anchor" href="#%e4%ba%8b%e5%8a%a1%e6%93%8d%e4%bd%9c">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">开启事务：</span>start transaction或begin [work]
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">提交事务：</span>commit
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">回滚事务：</span>rollback
</span></span></code></pre></div><p>例题：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> delimiter <span style="color:#f92672">//</span>   <span style="color:#75715e"># 更改定界符
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> start transaction;			<span style="color:#75715e"># 开启事务
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">update</span> bank <span style="color:#66d9ef">set</span> money<span style="color:#f92672">=</span>money<span style="color:#f92672">-</span><span style="color:#ae81ff">100</span> <span style="color:#66d9ef">where</span> cardid<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;1001&#39;</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">update</span> bank <span style="color:#66d9ef">set</span> money<span style="color:#f92672">=</span>money<span style="color:#f92672">+</span><span style="color:#ae81ff">100</span> <span style="color:#66d9ef">where</span> cardid<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;1002&#39;</span>  <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> commit <span style="color:#f92672">//</span>   <span style="color:#75715e"># 提交事务mysql&gt; rollback //  # 回滚事务
</span></span></span></code></pre></div><pre tabindex="0"><code>思考：事务什么时候产生？什么时候结束？答：开启的时候产生，提交事务或回滚事务都结束脚下留心：只有innodb和BDB才支持事务，myisam不支持事务。
</code></pre><h4 id="设置事务的回滚点">
  设置事务的回滚点
  <a class="anchor" href="#%e8%ae%be%e7%bd%ae%e4%ba%8b%e5%8a%a1%e7%9a%84%e5%9b%9e%e6%bb%9a%e7%82%b9">#</a>
</h4>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">设置回滚点：</span> savepoint <span style="color:#960050;background-color:#1e0010">回滚点名</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">回滚到回滚点：</span> rollback <span style="color:#66d9ef">to</span> <span style="color:#960050;background-color:#1e0010">回滚点</span>
</span></span></code></pre></div><p>例题：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span>  start transaction;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> bank <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;1003&#39;</span>,<span style="color:#ae81ff">1000</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> savepoint aa;    <span style="color:#75715e"># 设置回滚点 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>aaQuery OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">insert</span> <span style="color:#66d9ef">into</span> bank <span style="color:#66d9ef">values</span> (<span style="color:#e6db74">&#39;1004&#39;</span>,<span style="color:#ae81ff">500</span>);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">1</span> row <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec) 
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> savepoint bb;   <span style="color:#75715e"># 设置回滚点bb
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec) 
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> rollback <span style="color:#66d9ef">to</span> aa;    <span style="color:#75715e"># 回滚到aa点
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> commit;    <span style="color:#75715e"># 提交事务
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> bank ;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> cardid <span style="color:#f92672">|</span> money <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+-------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">1001</span>   <span style="color:#f92672">|</span>   <span style="color:#ae81ff">800</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">1002</span>   <span style="color:#f92672">|</span>   <span style="color:#ae81ff">200</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">1003</span>   <span style="color:#f92672">|</span>  <span style="color:#ae81ff">1000</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+-------+</span>
</span></span></code></pre></div><h4 id="事务的特性acid">
  事务的特性（ACID）
  <a class="anchor" href="#%e4%ba%8b%e5%8a%a1%e7%9a%84%e7%89%b9%e6%80%a7acid">#</a>
</h4>
<ol>
<li>原子性（Atomicity）：事务是一个整体，不可以再分，要么一起执行，要么一起不执行。</li>
<li>一致性（Consistency）：事务完成时，数据必须处于一致的状态。</li>
<li>隔离性（Isolation）：每个事务都是相互隔离的</li>
<li>永久性（Durability）：事务完成后，对数据的修改是永久性的。</li>
</ol>
<h2 id="索引index">
  索引【index】
  <a class="anchor" href="#%e7%b4%a2%e5%bc%95index">#</a>
</h2>
<p>索引的优点：查询速度快</p>
<p>索引的缺点：</p>
<ol>
<li>增、删、改（数据操作语句）效率低了</li>
<li>索引占用空间</li>
</ol>
<h4 id="索引的类型">
  索引的类型
  <a class="anchor" href="#%e7%b4%a2%e5%bc%95%e7%9a%84%e7%b1%bb%e5%9e%8b">#</a>
</h4>
<ol>
<li>
<p>普通索引</p>
</li>
<li>
<p>唯一索引（唯一键）</p>
</li>
<li>
<p>主键索引：只要主键就自动创建主键索引，不需要手动创建。</p>
</li>
<li>
<p>全文索引，搜索引擎使用，MySQL不支持中文的全文索引，我们通过sphinx去解决中文的全文索引。</p>
</li>
</ol>
<h4 id="创建普通索引">
  创建普通索引
  <a class="anchor" href="#%e5%88%9b%e5%bb%ba%e6%99%ae%e9%80%9a%e7%b4%a2%e5%bc%95">#</a>
</h4>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">create</span> <span style="color:#66d9ef">index</span> [<span style="color:#960050;background-color:#1e0010">索引名</span>] <span style="color:#66d9ef">on</span> <span style="color:#960050;background-color:#1e0010">表名</span> <span style="color:#960050;background-color:#1e0010">（字段名）</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> <span style="color:#960050;background-color:#1e0010">表名</span> <span style="color:#66d9ef">add</span> <span style="color:#66d9ef">index</span> [<span style="color:#960050;background-color:#1e0010">索引的名称</span>] <span style="color:#960050;background-color:#1e0010">（列名）</span>
</span></span></code></pre></div><p>例题：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#75715e"># 创建索引方法一
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">index</span> ix_stuname <span style="color:#66d9ef">on</span> <span style="color:#a6e22e">stuinfo</span>(stuname);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">08</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 创建索引方法二
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> stuinfo <span style="color:#66d9ef">add</span> <span style="color:#66d9ef">index</span> <span style="color:#a6e22e">ix_address</span> (stuaddress);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">08</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 创建表的时候就添加索引
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">emp</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>),
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">index</span> <span style="color:#a6e22e">ix_name</span> (name)   <span style="color:#75715e"># 创建索引
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="创建唯一索引">
  创建唯一索引
  <a class="anchor" href="#%e5%88%9b%e5%bb%ba%e5%94%af%e4%b8%80%e7%b4%a2%e5%bc%95">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法一：</span><span style="color:#66d9ef">create</span> <span style="color:#66d9ef">unique</span> <span style="color:#66d9ef">index</span> <span style="color:#960050;background-color:#1e0010">索引名</span> <span style="color:#66d9ef">on</span> <span style="color:#960050;background-color:#1e0010">表名</span> <span style="color:#960050;background-color:#1e0010">（字段名）</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法二：</span><span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> <span style="color:#960050;background-color:#1e0010">表名</span> <span style="color:#66d9ef">add</span> unqiue [<span style="color:#66d9ef">index</span>] [<span style="color:#960050;background-color:#1e0010">索引的名称</span>] <span style="color:#960050;background-color:#1e0010">（列名）</span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">语法三：创建表的时候添加唯一索引，和创建唯一键是一样的。</span>
</span></span></code></pre></div><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#75715e"># 方法一：
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">unique</span> <span style="color:#66d9ef">index</span> UQ_stuname <span style="color:#66d9ef">on</span> <span style="color:#a6e22e">stu</span>(stuname);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">06</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 方法二：
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">alter</span> <span style="color:#66d9ef">table</span> stu <span style="color:#66d9ef">add</span> <span style="color:#66d9ef">unique</span> <span style="color:#a6e22e">UQ_address</span> (stuaddress);
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">02</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 方法三
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">table</span> <span style="color:#a6e22e">stu2</span>(
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> id <span style="color:#66d9ef">int</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">20</span>),
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">unique</span> <span style="color:#a6e22e">UQ_name</span>(name)
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> );
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">01</span> sec)
</span></span></code></pre></div><h4 id="删除索引">
  删除索引
  <a class="anchor" href="#%e5%88%a0%e9%99%a4%e7%b4%a2%e5%bc%95">#</a>
</h4>
<p>语法</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">index</span> <span style="color:#960050;background-color:#1e0010">索引名</span> <span style="color:#66d9ef">on</span> <span style="color:#960050;background-color:#1e0010">表名</span>
</span></span></code></pre></div><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">index</span> ix_stuname <span style="color:#66d9ef">on</span> stuinfo;
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">03</span> sec)
</span></span><span style="display:flex;"><span>Records: <span style="color:#ae81ff">0</span>  Duplicates: <span style="color:#ae81ff">0</span>  Warnings: <span style="color:#ae81ff">0</span>
</span></span></code></pre></div><h4 id="创建索引的指导原则">
  创建索引的指导原则
  <a class="anchor" href="#%e5%88%9b%e5%bb%ba%e7%b4%a2%e5%bc%95%e7%9a%84%e6%8c%87%e5%af%bc%e5%8e%9f%e5%88%99">#</a>
</h4>
<ol>
<li>
<p>该列用于频繁搜索</p>
</li>
<li>
<p>改列用于排序</p>
</li>
<li>
<p>公共字段要创建索引</p>
</li>
<li>
<p>如果表中的数据很少，不需要创建索引。MySQL搜索索引的时间比逐条搜索数据的时间要长。</p>
</li>
<li>
<p>如果一个字段上的数据只有几个不同的值，改字段不适合做索引，比如性别。</p>
</li>
</ol>
<h2 id="函数">
  函数
  <a class="anchor" href="#%e5%87%bd%e6%95%b0">#</a>
</h2>
<h4 id="数字类">
  数字类
  <a class="anchor" href="#%e6%95%b0%e5%ad%97%e7%b1%bb">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">rand</span>();			<span style="color:#75715e"># 生成随机数
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">rand</span>()              <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">18474003969201822</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> <span style="color:#a6e22e">rand</span>();   <span style="color:#75715e"># 随机排序
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">order</span> <span style="color:#66d9ef">by</span> <span style="color:#a6e22e">rand</span>() <span style="color:#66d9ef">limit</span> <span style="color:#ae81ff">2</span>;    <span style="color:#75715e"># 随机抽两个学生
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25305 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">7</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河南</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">round</span>(<span style="color:#ae81ff">3</span>.<span style="color:#ae81ff">5</span>);     <span style="color:#75715e">#四舍五入
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">round</span>(<span style="color:#ae81ff">3</span>.<span style="color:#ae81ff">5</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>          <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">ceil</span>(<span style="color:#ae81ff">3</span>.<span style="color:#ae81ff">1</span>);	<span style="color:#75715e"># 向上取整
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+-----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">ceil</span>(<span style="color:#ae81ff">3</span>.<span style="color:#ae81ff">1</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>         <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">floor</span>(<span style="color:#ae81ff">3</span>.<span style="color:#ae81ff">9</span>);	<span style="color:#75715e"># 向下取整
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">floor</span>(<span style="color:#ae81ff">3</span>.<span style="color:#ae81ff">9</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>          <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">truncate</span>(<span style="color:#ae81ff">3</span>.<span style="color:#ae81ff">1415926</span>,<span style="color:#ae81ff">3</span>);	<span style="color:#75715e"># 截取数字
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+-----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">truncate</span>(<span style="color:#ae81ff">3</span>.<span style="color:#ae81ff">1415926</span>,<span style="color:#ae81ff">3</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>                 <span style="color:#ae81ff">3</span>.<span style="color:#ae81ff">141</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="字符串类">
  字符串类
  <a class="anchor" href="#%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%b1%bb">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">ucase</span>(<span style="color:#e6db74">&#39;i am a boy!&#39;</span>);		<span style="color:#75715e"># 转成大写
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">ucase</span>(<span style="color:#e6db74">&#39;i am a boy!&#39;</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> I AM A BOY<span style="color:#f92672">!</span>          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">lcase</span>(<span style="color:#e6db74">&#39;I Am A Boy!&#39;</span>);		<span style="color:#75715e">#转成小写
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">lcase</span>(<span style="color:#e6db74">&#39;I Am A Boy!&#39;</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> i am a boy<span style="color:#f92672">!</span>          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#66d9ef">left</span>(<span style="color:#e6db74">&#39;abcde&#39;</span>,<span style="color:#ae81ff">3</span>);		<span style="color:#75715e"># 从左边开始截取，截取3个
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+-----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">left</span>(<span style="color:#e6db74">&#39;abcde&#39;</span>,<span style="color:#ae81ff">3</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> abc             <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#66d9ef">right</span>(<span style="color:#e6db74">&#39;abcde&#39;</span>,<span style="color:#ae81ff">3</span>);		<span style="color:#75715e"># 从右边开始截取，截取3个
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">right</span>(<span style="color:#e6db74">&#39;abcde&#39;</span>,<span style="color:#ae81ff">3</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> cde              <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">substring</span>(<span style="color:#e6db74">&#39;abcde&#39;</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>);	<span style="color:#75715e">#从第2个位置开始截取，截取3个【位置从1开始】
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">substring</span>(<span style="color:#e6db74">&#39;abcde&#39;</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> bcd                    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">concat</span>(<span style="color:#e6db74">&#39;中国&#39;</span>,<span style="color:#e6db74">&#39;上海&#39;</span>);	<span style="color:#75715e"># 字符串相连
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+-----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">concat</span>(<span style="color:#e6db74">&#39;中国&#39;</span>,<span style="color:#e6db74">&#39;上海&#39;</span>)       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">中国上海</span>                    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+-----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">concat</span>(stuname,<span style="color:#e6db74">&#39;-&#39;</span>,stusex) <span style="color:#66d9ef">from</span> stuinfo;  <span style="color:#75715e"># 将表中的姓名和性别连接起来
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+----------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">concat</span>(stuname,<span style="color:#e6db74">&#39;-&#39;</span>,stusex) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span><span style="color:#f92672">-</span><span style="color:#960050;background-color:#1e0010">男</span>                    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span><span style="color:#f92672">-</span><span style="color:#960050;background-color:#1e0010">男</span>                    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span><span style="color:#f92672">-</span><span style="color:#960050;background-color:#1e0010">女</span>                    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span><span style="color:#f92672">-</span><span style="color:#960050;background-color:#1e0010">男</span>                  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span><span style="color:#f92672">-</span><span style="color:#960050;background-color:#1e0010">女</span>                  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span><span style="color:#f92672">-</span><span style="color:#960050;background-color:#1e0010">男</span>                  <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span><span style="color:#f92672">-</span><span style="color:#960050;background-color:#1e0010">女</span>                    <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">7</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># coalesce(字段1，字段2)  如果字段1不为空就显示字段1，否则，显示字段2
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuname,<span style="color:#a6e22e">coalesce</span>(writtenexam,<span style="color:#e6db74">&#39;缺考&#39;</span>),<span style="color:#a6e22e">coalesce</span>(labexam,<span style="color:#e6db74">&#39;缺考&#39;</span>) <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">natural</span> <span style="color:#66d9ef">left</span> <span style="color:#66d9ef">join</span> stumarks;   <span style="color:#75715e"># 将考试成绩为空的显示为缺考
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+----------+------------------------------+--------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span> <span style="color:#a6e22e">coalesce</span>(writtenexam,<span style="color:#e6db74">&#39;缺考&#39;</span>)  <span style="color:#f92672">|</span> <span style="color:#a6e22e">coalesce</span>(labexam,<span style="color:#e6db74">&#39;缺考&#39;</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------------------------------+--------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>    <span style="color:#f92672">|</span> <span style="color:#ae81ff">77</span>                           <span style="color:#f92672">|</span> <span style="color:#ae81ff">82</span>                       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>    <span style="color:#f92672">|</span> <span style="color:#ae81ff">50</span>                           <span style="color:#f92672">|</span> <span style="color:#ae81ff">90</span>                       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>    <span style="color:#f92672">|</span> <span style="color:#ae81ff">88</span>                           <span style="color:#f92672">|</span> <span style="color:#ae81ff">58</span>                       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>   <span style="color:#f92672">|</span> <span style="color:#ae81ff">65</span>                           <span style="color:#f92672">|</span> <span style="color:#ae81ff">50</span>                       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">缺考</span>                          <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">缺考</span>                     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>   <span style="color:#f92672">|</span> <span style="color:#ae81ff">56</span>                           <span style="color:#f92672">|</span> <span style="color:#ae81ff">48</span>                       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">缺考</span>                          <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">缺考</span>                     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------------------------------+--------------------------+</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">length</span>(<span style="color:#e6db74">&#39;锄禾日当午&#39;</span>);		<span style="color:#75715e"># 字节长度
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">length</span>(<span style="color:#e6db74">&#39;锄禾日当午&#39;</span>)   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>                   <span style="color:#ae81ff">10</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">char_length</span>(<span style="color:#e6db74">&#39;锄禾日当午&#39;</span>);		<span style="color:#75715e"># 字符个数
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+---------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">char_length</span>(<span style="color:#e6db74">&#39;锄禾日当午&#39;</span>)   <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>                         <span style="color:#ae81ff">5</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="时间类">
  时间类
  <a class="anchor" href="#%e6%97%b6%e9%97%b4%e7%b1%bb">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">unix_timestamp</span>();	<span style="color:#75715e">#获取时间戳
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">unix_timestamp</span>() <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>       <span style="color:#ae81ff">1537084508</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">from_unixtime</span>(<span style="color:#a6e22e">unix_timestamp</span>());	<span style="color:#75715e"># 将时间戳转成年-月-日 小时:分钟:秒的格式
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+---------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">from_unixtime</span>(<span style="color:#a6e22e">unix_timestamp</span>()) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">2018</span><span style="color:#f92672">-</span><span style="color:#ae81ff">09</span><span style="color:#f92672">-</span><span style="color:#ae81ff">16</span> <span style="color:#ae81ff">15</span>:<span style="color:#ae81ff">55</span>:<span style="color:#ae81ff">56</span>             <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">now</span>();		<span style="color:#75715e"># 获取当前日期时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">now</span>()               <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">2018</span><span style="color:#f92672">-</span><span style="color:#ae81ff">09</span><span style="color:#f92672">-</span><span style="color:#ae81ff">16</span> <span style="color:#ae81ff">15</span>:<span style="color:#ae81ff">57</span>:<span style="color:#ae81ff">04</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#66d9ef">year</span>(<span style="color:#a6e22e">now</span>()) <span style="color:#960050;background-color:#1e0010">年</span>,<span style="color:#a6e22e">month</span>(<span style="color:#a6e22e">now</span>()) <span style="color:#960050;background-color:#1e0010">月</span>, <span style="color:#a6e22e">day</span>(<span style="color:#a6e22e">now</span>()) <span style="color:#960050;background-color:#1e0010">日</span>,<span style="color:#a6e22e">hour</span>(<span style="color:#a6e22e">now</span>()) <span style="color:#960050;background-color:#1e0010">小</span>,<span style="color:#a6e22e">minute</span>(<span style="color:#a6e22e">now</span>()) <span style="color:#960050;background-color:#1e0010">分钟</span>,<span style="color:#a6e22e">second</span>(<span style="color:#a6e22e">now</span>()) <span style="color:#960050;background-color:#1e0010">秒</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+------+------+------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">年</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">月</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">日</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">小时</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">分钟</span>     <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">秒</span>     <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+------+------+------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">2018</span> <span style="color:#f92672">|</span>    <span style="color:#ae81ff">9</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">16</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">15</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">59</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">14</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+------+------+------+------+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">dayname</span>(<span style="color:#a6e22e">now</span>()) <span style="color:#960050;background-color:#1e0010">星期</span>,<span style="color:#a6e22e">monthname</span>(<span style="color:#a6e22e">now</span>()),<span style="color:#a6e22e">dayofyear</span>(<span style="color:#a6e22e">now</span>()) <span style="color:#960050;background-color:#1e0010">本年的第几天</span>;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+------------------+--------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">星期</span>       <span style="color:#f92672">|</span> <span style="color:#a6e22e">monthname</span>(<span style="color:#a6e22e">now</span>()) <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">本年的第几天</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+------------------+--------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Sunday <span style="color:#f92672">|</span> September        <span style="color:#f92672">|</span>          <span style="color:#ae81ff">259</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+------------------+--------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">datediff</span>(<span style="color:#a6e22e">now</span>(),<span style="color:#e6db74">&#39;2008-8-8&#39;</span>);	<span style="color:#75715e"># 日期相减
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+----------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">datediff</span>(<span style="color:#a6e22e">now</span>(),<span style="color:#e6db74">&#39;2008-8-8&#39;</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>                       <span style="color:#ae81ff">3691</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#66d9ef">convert</span>(<span style="color:#a6e22e">now</span>(),<span style="color:#66d9ef">date</span>),<span style="color:#66d9ef">convert</span>(<span style="color:#a6e22e">now</span>(),<span style="color:#66d9ef">time</span>);	<span style="color:#75715e"># 将now()转成日期和时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+---------------------+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">convert</span>(<span style="color:#a6e22e">now</span>(),<span style="color:#66d9ef">date</span>) <span style="color:#f92672">|</span> <span style="color:#66d9ef">convert</span>(<span style="color:#a6e22e">now</span>(),<span style="color:#66d9ef">time</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">2018</span><span style="color:#f92672">-</span><span style="color:#ae81ff">09</span><span style="color:#f92672">-</span><span style="color:#ae81ff">16</span>          <span style="color:#f92672">|</span> <span style="color:#ae81ff">16</span>:<span style="color:#ae81ff">07</span>:<span style="color:#ae81ff">24</span>            <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+---------------------+</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#a6e22e">cast</span>(<span style="color:#a6e22e">now</span>() <span style="color:#66d9ef">as</span> <span style="color:#66d9ef">date</span>),<span style="color:#a6e22e">cast</span>(<span style="color:#a6e22e">now</span>() <span style="color:#66d9ef">as</span> <span style="color:#66d9ef">time</span>);   <span style="color:#75715e"># 将now()转成日期和时间
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+---------------------+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">cast</span>(<span style="color:#a6e22e">now</span>() <span style="color:#66d9ef">as</span> <span style="color:#66d9ef">date</span>) <span style="color:#f92672">|</span> <span style="color:#a6e22e">cast</span>(<span style="color:#a6e22e">now</span>() <span style="color:#66d9ef">as</span> <span style="color:#66d9ef">time</span>) <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">2018</span><span style="color:#f92672">-</span><span style="color:#ae81ff">09</span><span style="color:#f92672">-</span><span style="color:#ae81ff">16</span>          <span style="color:#f92672">|</span> <span style="color:#ae81ff">16</span>:<span style="color:#ae81ff">08</span>:<span style="color:#ae81ff">03</span>            <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------------------+---------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="加密函数">
  加密函数
  <a class="anchor" href="#%e5%8a%a0%e5%af%86%e5%87%bd%e6%95%b0">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#f92672">+----------------------------------+------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#a6e22e">md5</span>(<span style="color:#e6db74">&#39;root&#39;</span>)                      <span style="color:#f92672">|</span> <span style="color:#a6e22e">sha</span>(<span style="color:#e6db74">&#39;root&#39;</span>)                              <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------------------+------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#ae81ff">63</span>a9f0ea7bb98050796b649e85481845 <span style="color:#f92672">|</span> dc76e9f0c0006e8f919e0c515c66dbba3982f785 <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------------------------------+------------------------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="判断函数">
  判断函数
  <a class="anchor" href="#%e5%88%a4%e6%96%ad%e5%87%bd%e6%95%b0">#</a>
</h4>
<p>语法</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">if</span>(<span style="color:#960050;background-color:#1e0010">表达式</span>,<span style="color:#960050;background-color:#1e0010">值</span><span style="color:#ae81ff">1</span>,<span style="color:#960050;background-color:#1e0010">值</span><span style="color:#ae81ff">2</span>)
</span></span></code></pre></div><p>例题：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#66d9ef">if</span>(<span style="color:#ae81ff">10</span><span style="color:#f92672">%</span><span style="color:#ae81ff">2</span><span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>,<span style="color:#e6db74">&#39;偶数&#39;</span>,<span style="color:#e6db74">&#39;奇数&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#66d9ef">if</span>(<span style="color:#ae81ff">10</span><span style="color:#f92672">%</span><span style="color:#ae81ff">2</span><span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>,<span style="color:#e6db74">&#39;偶数&#39;</span>,<span style="color:#e6db74">&#39;奇数&#39;</span>)        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">偶数</span>                        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------------------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 语文和数学都超过60分才通过
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> stuname,ch,math,<span style="color:#66d9ef">if</span>(ch<span style="color:#f92672">&gt;=</span><span style="color:#ae81ff">60</span> <span style="color:#f92672">&amp;&amp;</span> math<span style="color:#f92672">&gt;=</span><span style="color:#ae81ff">60</span>,<span style="color:#e6db74">&#39;通过&#39;</span>,<span style="color:#e6db74">&#39;不通过&#39;</span>) <span style="color:#e6db74">&#39;是否通过&#39;</span> <span style="color:#66d9ef">from</span> stu;
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------+------+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuname  <span style="color:#f92672">|</span> ch   <span style="color:#f92672">|</span> math <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">是否通过</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------+------+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span>   <span style="color:#ae81ff">80</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">不通过</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">76</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">通过</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">55</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">82</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">不通过</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span> <span style="color:#66d9ef">NULL</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">74</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">不通过</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>         <span style="color:#f92672">|</span>   <span style="color:#ae81ff">72</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">56</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">不通过</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">86</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">92</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">通过</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>        <span style="color:#f92672">|</span>   <span style="color:#ae81ff">74</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">67</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">通过</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Tom      <span style="color:#f92672">|</span>   <span style="color:#ae81ff">65</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">67</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">通过</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> Tabm     <span style="color:#f92672">|</span>   <span style="color:#ae81ff">88</span> <span style="color:#f92672">|</span>   <span style="color:#ae81ff">77</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">通过</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+----------+------+------+----------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">9</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h2 id="预处理">
  预处理
  <a class="anchor" href="#%e9%a2%84%e5%a4%84%e7%90%86">#</a>
</h2>
<p>预编译一次，可以多次执行。用来解决一条SQL语句频繁执行的问题。</p>
<pre tabindex="0"><code>预处理语句：prepare 预处理名字 from ‘sql语句’
执行预处理：execute 预处理名字 [using 变量]
</code></pre><p>例题一：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> prepare stmt <span style="color:#66d9ef">from</span> <span style="color:#e6db74">&#39;select * from stuinfo&#39;</span>;	<span style="color:#75715e"># 创建预处理
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Statement prepared
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> execute stmt;	<span style="color:#75715e"># 执行预处理
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25305 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">7</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河南</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25319 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">5</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">7</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>例题二：传递参数</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> delimiter <span style="color:#f92672">//</span> 
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> prepare stmt <span style="color:#66d9ef">from</span> <span style="color:#e6db74">&#39;select * from stuinfo where stuno=?&#39;</span> <span style="color:#f92672">//</span> <span style="color:#75715e">-- ?是位置占位符
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Statement prepared
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">set</span> <span style="color:#f92672">@</span>id<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;s25301&#39;</span>;       <span style="color:#75715e">-- 变量以@开头，通过set给变量赋值
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> execute stmt <span style="color:#66d9ef">using</span> <span style="color:#f92672">@</span>id <span style="color:#f92672">//</span>  <span style="color:#75715e">-- 执行预处理，传递参数
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><pre tabindex="0"><code>脚下留心：
1、?是位置占位符
2、变量以@开头
3、通过set给变量赋值
</code></pre><p>例题三：传递多个参数</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> prepare stmt <span style="color:#66d9ef">from</span> <span style="color:#e6db74">&#39;select * from stuinfo where stusex=? and stuaddress=?&#39;</span>  <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>Statement prepared
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">set</span> <span style="color:#f92672">@</span>sex<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;男&#39;</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">set</span> <span style="color:#f92672">@</span>addr<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;北京&#39;</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> execute stmt <span style="color:#66d9ef">using</span> <span style="color:#f92672">@</span>sex,<span style="color:#f92672">@</span>addr <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h2 id="存储过程procedure">
  存储过程procedure
  <a class="anchor" href="#%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8bprocedure">#</a>
</h2>
<h4 id="存储过程的优点">
  存储过程的优点
  <a class="anchor" href="#%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e7%9a%84%e4%bc%98%e7%82%b9">#</a>
</h4>
<ol>
<li>存储过程可以减少网络流量</li>
<li>允许模块化设计</li>
<li>支持事务</li>
</ol>
<h4 id="创建存储过程">
  创建存储过程
  <a class="anchor" href="#%e5%88%9b%e5%bb%ba%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b">#</a>
</h4>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">create</span> <span style="color:#66d9ef">procedure</span> <span style="color:#960050;background-color:#1e0010">存储过程名</span>(<span style="color:#960050;background-color:#1e0010">参数</span>)
</span></span><span style="display:flex;"><span>begin
</span></span><span style="display:flex;"><span>	<span style="color:#f92672">//</span>sql语句
</span></span><span style="display:flex;"><span>end;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#960050;background-color:#1e0010">脚下留心：由于过程中有很多</span>SQL语句<span style="color:#960050;background-color:#1e0010">，每个语句的结束都要用（；）结束。默认情况下，分号既表示语句结束，又表示向服务器发送</span>SQL语句<span style="color:#960050;background-color:#1e0010">。我们希望分号仅表示语句的结束，不要将</span>SQL语句发送到服务器执行<span style="color:#960050;background-color:#1e0010">，通过</span>delimiter来更改结束符<span style="color:#960050;background-color:#1e0010">。</span>
</span></span></code></pre></div><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> delimiter <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">procedure</span> <span style="color:#a6e22e">proc</span>()     <span style="color:#75715e">-- 创建存储过程
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> begin
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo;
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> end <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="调用存储过程">
  调用存储过程
  <a class="anchor" href="#%e8%b0%83%e7%94%a8%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b">#</a>
</h4>
<p>语法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">call</span> <span style="color:#960050;background-color:#1e0010">存储过程名</span>()
</span></span></code></pre></div><p>例题：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">call</span> <span style="color:#a6e22e">proc</span>() <span style="color:#f92672">//</span>     <span style="color:#75715e">-- 调用存储过程
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25302 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李文才</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">31</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">3</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">上海</span>          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25305 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">诸葛丽丽</span>         <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">7</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河南</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25318 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">争青小子</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">26</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">6</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>           <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25319 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">梅超风</span>        <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">23</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">5</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">河北</span>          <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">7</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="删除存储过程">
  删除存储过程
  <a class="anchor" href="#%e5%88%a0%e9%99%a4%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b">#</a>
</h4>
<p>语法</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">procedure</span> [<span style="color:#66d9ef">if</span> <span style="color:#66d9ef">exists</span>] <span style="color:#960050;background-color:#1e0010">存储过程名</span>
</span></span></code></pre></div><p>例题：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">drop</span> <span style="color:#66d9ef">procedure</span> proc <span style="color:#f92672">//</span>    <span style="color:#75715e">-- 删除存储过程
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="查看存储过程的信息">
  查看存储过程的信息
  <a class="anchor" href="#%e6%9f%a5%e7%9c%8b%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e7%9a%84%e4%bf%a1%e6%81%af">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span><span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">procedure</span> <span style="color:#960050;background-color:#1e0010">存储过程名\</span>G
</span></span></code></pre></div><p>例题</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">procedure</span> proc <span style="color:#960050;background-color:#1e0010">\</span>G
</span></span><span style="display:flex;"><span><span style="color:#f92672">***************************</span> <span style="color:#ae81ff">1</span>. row <span style="color:#f92672">***************************</span>
</span></span><span style="display:flex;"><span>           <span style="color:#66d9ef">Procedure</span>: proc
</span></span><span style="display:flex;"><span>            sql_mode: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">Create</span> <span style="color:#66d9ef">Procedure</span>: <span style="color:#66d9ef">CREATE</span> DEFINER<span style="color:#f92672">=`</span>root<span style="color:#f92672">`@`</span>localhost<span style="color:#f92672">`</span> <span style="color:#66d9ef">PROCEDURE</span> <span style="color:#f92672">`</span>proc<span style="color:#f92672">`</span>()
</span></span><span style="display:flex;"><span>begin
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo;
</span></span><span style="display:flex;"><span>end
</span></span><span style="display:flex;"><span>character_set_client: gbk
</span></span><span style="display:flex;"><span>collation_connection: gbk_chinese_ci
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">Database</span> Collation: utf8_general_ci
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h4 id="显示所有的存储过程">
  显示所有的存储过程
  <a class="anchor" href="#%e6%98%be%e7%a4%ba%e6%89%80%e6%9c%89%e7%9a%84%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b">#</a>
</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">show</span> <span style="color:#66d9ef">procedure</span> status <span style="color:#960050;background-color:#1e0010">\</span>G
</span></span></code></pre></div><h4 id="存储过程的参数">
  存储过程的参数
  <a class="anchor" href="#%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e7%9a%84%e5%8f%82%e6%95%b0">#</a>
</h4>
<p>存储过程的参数分为：输入参数（in）【默认】，输出参数（out），输入输出参数（inout）</p>
<p>存储过程不能使用return返回值，要返回值只能通过“输出参数”来向外传递值。</p>
<p>例题一：传递学号，获取对应的信息</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">procedure</span> <span style="color:#a6e22e">proc</span>(<span style="color:#66d9ef">in</span> param <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>))   <span style="color:#75715e">-- 输入参数
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> stuno<span style="color:#f92672">=</span>param <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">call</span> <span style="color:#a6e22e">proc</span>(<span style="color:#e6db74">&#39;s25301&#39;</span>) <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25301 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">张秋丽</span>   <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>     <span style="color:#f92672">|</span>     <span style="color:#ae81ff">18</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">1</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>        <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+---------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>例题二：查找同桌</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">procedure</span> <span style="color:#a6e22e">proc</span>(name <span style="color:#66d9ef">varchar</span>(<span style="color:#ae81ff">10</span>))
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> begin
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">declare</span> seat <span style="color:#66d9ef">tinyint</span>;   <span style="color:#75715e">-- 声明局部变量
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">select</span> stuseat <span style="color:#66d9ef">into</span> seat <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> stuname<span style="color:#f92672">=</span>name;  <span style="color:#75715e">-- 将座位号保存到变量中
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">from</span> stuinfo <span style="color:#66d9ef">where</span> stuseat<span style="color:#f92672">=</span>seat<span style="color:#f92672">+</span><span style="color:#ae81ff">1</span> <span style="color:#66d9ef">or</span> stuseat<span style="color:#f92672">=</span>seat<span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>;  <span style="color:#75715e">-- 查找同桌
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> end <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">call</span> <span style="color:#a6e22e">proc</span>(<span style="color:#e6db74">&#39;李文才&#39;</span>) <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> stuNo  <span style="color:#f92672">|</span> stuName  <span style="color:#f92672">|</span> stuSex <span style="color:#f92672">|</span> stuAge <span style="color:#f92672">|</span> stuSeat <span style="color:#f92672">|</span> stuAddress <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25303 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">李斯文</span>    <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">女</span>      <span style="color:#f92672">|</span>     <span style="color:#ae81ff">22</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">2</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">北京</span>       <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> s25304 <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">欧阳俊雄</span>  <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">男</span>       <span style="color:#f92672">|</span>     <span style="color:#ae81ff">28</span> <span style="color:#f92672">|</span>       <span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#960050;background-color:#1e0010">天津</span>      <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+--------+----------+--------+--------+---------+------------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">2</span> rows <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>强调</p>
<pre tabindex="0"><code>1、通过declare关键字声明局部变量；全局变量@开头就可以了
2、给变量赋值有两种方法
	方法一：set 变量名=值
	方法二：select 字段 into 变量 from 表 where 条件
3、声明的变量不能与列名同名
</code></pre><p>例题三：输出参数</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">procedure</span> <span style="color:#a6e22e">proc</span>(num <span style="color:#66d9ef">int</span>, <span style="color:#66d9ef">out</span> result <span style="color:#66d9ef">int</span>)  <span style="color:#f92672">//</span><span style="color:#66d9ef">out</span> <span style="color:#960050;background-color:#1e0010">表示输出参数</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> begin
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">set</span> result<span style="color:#f92672">=</span>num<span style="color:#f92672">*</span>num;
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> end <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">call</span> <span style="color:#a6e22e">proc</span>(<span style="color:#ae81ff">10</span>,<span style="color:#f92672">@</span>result) <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">@</span>result <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#f92672">@</span>result <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>     <span style="color:#ae81ff">100</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+---------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><p>例题四：输入输出参数</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-mysql" data-lang="mysql"><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">create</span> <span style="color:#66d9ef">procedure</span> <span style="color:#a6e22e">proc</span>(<span style="color:#66d9ef">inout</span> num <span style="color:#66d9ef">int</span>)  <span style="color:#75715e">#  inout 表示是输入输出参数
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">-&gt;</span> begin
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">set</span> num<span style="color:#f92672">=</span>num<span style="color:#f92672">*</span>num;
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> end <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>mysql<span style="color:#f92672">&gt;</span> <span style="color:#66d9ef">set</span> <span style="color:#f92672">@</span>num<span style="color:#f92672">=</span><span style="color:#ae81ff">10</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">call</span> <span style="color:#a6e22e">proc</span>(<span style="color:#f92672">@</span>num);
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">-&gt;</span> <span style="color:#66d9ef">select</span> <span style="color:#f92672">@</span>num <span style="color:#f92672">//</span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Query OK, <span style="color:#ae81ff">0</span> rows <span style="color:#a6e22e">affected</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span> <span style="color:#f92672">@</span>num <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">|</span>  <span style="color:#ae81ff">100</span> <span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">+------+</span>
</span></span><span style="display:flex;"><span><span style="color:#ae81ff">1</span> row <span style="color:#66d9ef">in</span> <span style="color:#66d9ef">set</span> (<span style="color:#ae81ff">0</span>.<span style="color:#ae81ff">00</span> sec)
</span></span></code></pre></div><h2 id="go连接mysql">
  GO连接MySQL
  <a class="anchor" href="#go%e8%bf%9e%e6%8e%a5mysql">#</a>
</h2>
</article>
 
      

      <footer class="book-footer">
        
  <div class="flex flex-wrap justify-between">





</div>



  <script>(function(){function e(e){const t=window.getSelection(),n=document.createRange();n.selectNodeContents(e),t.removeAllRanges(),t.addRange(n)}document.querySelectorAll("pre code").forEach(t=>{t.addEventListener("click",function(){if(window.getSelection().toString())return;e(t.parentElement),navigator.clipboard&&navigator.clipboard.writeText(t.parentElement.textContent)})})})()</script>


 
        
      </footer>

      
  
  <div class="book-comments">

</div>
  
 

      <label for="menu-control" class="hidden book-menu-overlay"></label>
    </div>

    
    <aside class="book-toc">
      <div class="book-toc-content">
        
  
<nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#基础概念">基础概念</a>
          <ul>
            <li><a href="#数据库的发展史">数据库的发展史</a></li>
          </ul>
        </li>
        <li><a href="#mysql安装">MySql安装</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#数据库操作">数据库操作</a>
          <ul>
            <li><a href="#显示数据库">显示数据库</a></li>
            <li><a href="#创建数据库">创建数据库</a></li>
            <li><a href="#heading"></a></li>
            <li><a href="#删除数据库">删除数据库</a></li>
            <li><a href="#显示创建数据库的sql语句">显示创建数据库的SQL语句</a></li>
            <li><a href="#修改数据库">修改数据库</a></li>
            <li><a href="#选择数据库">选择数据库</a></li>
          </ul>
        </li>
        <li><a href="#表的操作">表的操作</a>
          <ul>
            <li><a href="#显示所有表">显示所有表</a></li>
            <li><a href="#创建表">创建表</a></li>
            <li><a href="#显示创建表的语句">显示创建表的语句</a></li>
            <li><a href="#查看表结构">查看表结构</a></li>
            <li><a href="#删除表">删除表</a></li>
            <li><a href="#修改表">修改表</a></li>
            <li><a href="#复制表">复制表</a></li>
          </ul>
        </li>
        <li><a href="#数据操作">数据操作</a>
          <ul>
            <li><a href="#插入数据">插入数据</a></li>
            <li><a href="#更新数据">更新数据</a></li>
            <li><a href="#删除数据">删除数据</a></li>
            <li><a href="#清空表">清空表</a></li>
            <li><a href="#查询表">查询表</a></li>
          </ul>
        </li>
        <li><a href="#sql分类">SQL分类</a>
          <ul>
            <li><a href="#数据表的文件介绍">数据表的文件介绍</a></li>
            <li><a href="#字符集">字符集</a></li>
          </ul>
        </li>
        <li><a href="#数据类型">数据类型</a>
          <ul>
            <li><a href="#值类型">值类型</a></li>
            <li><a href="#字符型">字符型</a></li>
            <li><a href="#枚举enum">枚举（enum）</a></li>
            <li><a href="#集合set">集合（set）</a></li>
            <li><a href="#日期类型">日期类型</a></li>
            <li><a href="#boolean">boolean</a></li>
          </ul>
        </li>
        <li><a href="#列属性">列属性</a>
          <ul>
            <li><a href="#null--not-null">(null | not null)</a></li>
            <li><a href="#默认值default">默认值default</a></li>
            <li><a href="#自动增长">自动增长</a></li>
            <li><a href="#主键">主键</a></li>
            <li><a href="#唯一键">唯一键</a></li>
            <li><a href="#备注comment">备注comment</a></li>
          </ul>
        </li>
        <li><a href="#数据完整性介绍">数据完整性介绍</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#引用完整性">引用完整性</a>
          <ul>
            <li></li>
            <li><a href="#外键操作">外键操作</a></li>
          </ul>
        </li>
        <li><a href="#数据库基本概念">数据库基本概念</a></li>
        <li><a href="#实体和实体之间的关系">实体和实体之间的关系</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#数据库设计的步骤">数据库设计的步骤</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#数据规范化">数据规范化</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#查询语句">查询语句</a>
          <ul>
            <li><a href="#字段表达式">字段表达式</a></li>
            <li><a href="#from子句">from子句</a></li>
            <li><a href="#dual表">dual表</a></li>
            <li><a href="#where子句">where子句</a></li>
            <li><a href="#in--not-in">in | not in</a></li>
            <li><a href="#betweenandnot-betweenand">between…and|not between…and</a></li>
            <li><a href="#is-null--is-not-null">is null | is not null</a></li>
            <li><a href="#聚合函数">聚合函数</a></li>
            <li><a href="#通配符">通配符</a></li>
            <li><a href="#模糊查询like">模糊查询（like）</a></li>
            <li><a href="#order-by排序">order by排序</a></li>
            <li><a href="#group-by-分组查询">group by 【分组查询】</a></li>
            <li><a href="#having条件">having条件</a></li>
            <li><a href="#limit">limit</a></li>
            <li><a href="#查询语句中的选项">查询语句中的选项</a></li>
          </ul>
        </li>
        <li><a href="#union联合">union（联合）</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#多表查询分类">多表查询分类</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#子查询">子查询</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#视图view">视图【view】</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#事务transaction">事务transaction</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#索引index">索引【index】</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#函数">函数</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#预处理">预处理</a></li>
        <li><a href="#存储过程procedure">存储过程procedure</a>
          <ul>
            <li></li>
          </ul>
        </li>
        <li><a href="#go连接mysql">GO连接MySQL</a></li>
      </ul>
    </li>
  </ul>
</nav>


 
      </div>
    </aside>
    
  </main>

  
</body>
</html>












